Template tagsยค
All following template tags are defined in
django_components.templatetags.component_tags
Import as
component_css_dependenciesยค
Marks location where CSS link tags should be rendered after the whole HTML has been generated.
Generally, this should be inserted into the <head>
tag of the HTML.
If the generated HTML does NOT contain any {% component_css_dependencies %}
tags, CSS links are by default inserted into the <head>
tag of the HTML. (See Default JS / CSS locations)
Note that there should be only one {% component_css_dependencies %}
for the whole HTML document. If you insert this tag multiple times, ALL CSS links will be duplicately inserted into ALL these places.
component_js_dependenciesยค
Marks location where JS link tags should be rendered after the whole HTML has been generated.
Generally, this should be inserted at the end of the <body>
tag of the HTML.
If the generated HTML does NOT contain any {% component_js_dependencies %}
tags, JS scripts are by default inserted at the end of the <body>
tag of the HTML. (See Default JS / CSS locations)
Note that there should be only one {% component_js_dependencies %}
for the whole HTML document. If you insert this tag multiple times, ALL JS scripts will be duplicately inserted into ALL these places.
componentยค
Renders one of the components that was previously registered with @register()
decorator.
The {% component %}
tag takes:
- Component's registered name as the first positional argument,
- Followed by any number of positional and keyword arguments.
The component name must be a string literal.
Inserting slot fillsยค
If the component defined any slots, you can "fill" these slots by placing the {% fill %}
tags within the {% component %}
tag:
{% component "my_table" rows=rows headers=headers %}
{% fill "pagination" %}
< 1 | 2 | 3 >
{% endfill %}
{% endcomponent %}
You can even nest {% fill %}
tags within {% if %}
, {% for %}
and other tags:
{% component "my_table" rows=rows headers=headers %}
{% if rows %}
{% fill "pagination" %}
< 1 | 2 | 3 >
{% endfill %}
{% endif %}
{% endcomponent %}
Isolating componentsยค
By default, components behave similarly to Django's {% include %}
, and the template inside the component has access to the variables defined in the outer template.
You can selectively isolate a component, using the only
flag, so that the inner template can access only the data that was explicitly passed to it:
Alternatively, you can set all components to be isolated by default, by setting context_behavior
to "isolated"
in your settings:
Omitting the component keywordยค
If you would like to omit the component
keyword, and simply refer to your components by their registered names:
You can do so by setting the "shorthand" Tag formatter in the settings:
fillยค
{% fill name: str, *, data: Optional[str] = None, fallback: Optional[str] = None, body: Union[str, django.utils.safestring.SafeString, django_components.slots.SlotFunc[~TSlotData], django_components.slots.Slot[~TSlotData], NoneType] = None, default: Optional[str] = None %}
{% endfill %}
Use {% fill %}
tag to insert content into component's slots.
{% fill %}
tag may be used only within a {% component %}..{% endcomponent %}
block, and raises a TemplateSyntaxError
if used outside of a component.
Args:
name
(str, required): Name of the slot to insert this content into. Use"default"
for the default slot.data
(str, optional): This argument allows you to access the data passed to the slot under the specified variable name. See Slot data.fallback
(str, optional): This argument allows you to access the original content of the slot under the specified variable name. See Slot fallback.
Example:
Access slot fallbackยค
Use the fallback
kwarg to access the original content of the slot.
The fallback
kwarg defines the name of the variable that will contain the slot's fallback content.
Read more about Slot fallback.
Component template:
Fill:
{% component "my_table" %}
{% fill "pagination" fallback="fallback" %}
<div class="my-class">
{{ fallback }}
</div>
{% endfill %}
{% endcomponent %}
Access slot dataยค
Use the data
kwarg to access the data passed to the slot.
The data
kwarg defines the name of the variable that will contain the slot's data.
Read more about Slot data.
Component template:
{# my_table.html #}
<table>
...
{% slot "pagination" pages=pages %}
< 1 | 2 | 3 >
{% endslot %}
</table>
Fill:
{% component "my_table" %}
{% fill "pagination" data="slot_data" %}
{% for page in slot_data.pages %}
<a href="{{ page.link }}">
{{ page.index }}
</a>
{% endfor %}
{% endfill %}
{% endcomponent %}
Using default slotยค
To access slot data and the fallback slot content on the default slot, use {% fill %}
with name
set to "default"
:
{% component "button" %}
{% fill name="default" data="slot_data" fallback="slot_fallback" %}
You clicked me {{ slot_data.count }} times!
{{ slot_fallback }}
{% endfill %}
{% endcomponent %}
Slot fills from Pythonยค
You can pass a slot fill from Python to a component by setting the body
kwarg on the {% fill %}
tag.
First pass a Slot
instance to the template with the get_template_data()
method:
from django_components import component, Slot
class Table(Component):
def get_template_data(self, args, kwargs, slots, context):
return {
"my_slot": Slot(lambda ctx: "Hello, world!"),
}
Then pass the slot to the {% fill %}
tag:
Warning
If you define both the body
kwarg and the {% fill %}
tag's body, an error will be raised.
html_attrsยค
Generate HTML attributes (key="value"
), combining data from multiple sources, whether its template variables or static text.
It is designed to easily merge HTML attributes passed from outside as well as inside the component.
Args:
attrs
(dict, optional): Optional dictionary that holds HTML attributes. On conflict, overrides values in thedefault
dictionary.default
(str, optional): Optional dictionary that holds HTML attributes. On conflict, is overriden with values in theattrs
dictionary.- Any extra kwargs will be appended to the corresponding keys
The attributes in attrs
and defaults
are merged and resulting dict is rendered as HTML attributes (key="value"
).
Extra kwargs (key=value
) are concatenated to existing keys. So if we have
Then
will result in class="my-class extra-class"
.
Example:
renders
See more usage examples in HTML attributes.
provideยค
The {% provide %}
tag is part of the "provider" part of the provide / inject feature.
Pass kwargs to this tag to define the provider's data.
Any components defined within the {% provide %}..{% endprovide %}
tags will be able to access this data with Component.inject()
.
This is similar to React's ContextProvider
, or Vue's provide()
.
Args:
name
(str, required): Provider name. This is the name you will then use inComponent.inject()
.**kwargs
: Any extra kwargs will be passed as the provided data.
Example:
Provide the "user_data" in parent component:
@register("parent")
class Parent(Component):
template = """
<div>
{% provide "user_data" user=user %}
{% component "child" / %}
{% endprovide %}
</div>
"""
def get_template_data(self, args, kwargs, slots, context):
return {
"user": kwargs["user"],
}
Since the "child" component is used within the {% provide %} / {% endprovide %}
tags, we can request the "user_data" using Component.inject("user_data")
:
@register("child")
class Child(Component):
template = """
<div>
User is: {{ user }}
</div>
"""
def get_template_data(self, args, kwargs, slots, context):
user = self.inject("user_data").user
return {
"user": user,
}
Notice that the keys defined on the {% provide %}
tag are then accessed as attributes when accessing them with Component.inject()
.
โ Do this
โ Don't do this
slotยค
{% slot %}
tag marks a place inside a component where content can be inserted from outside.
Learn more about using slots.
This is similar to slots as seen in Web components, Vue or React's children
.
Args:
name
(str, required): Registered name of the component to renderdefault
: Optional flag. If there is a default slot, you can pass the component slot content without using the{% fill %}
tag. See Default slotrequired
: Optional flag. Will raise an error if a slot is required but not given.**kwargs
: Any extra kwargs will be passed as the slot data.
Example:
@register("child")
class Child(Component):
template = """
<div>
{% slot "content" default %}
This is shown if not overriden!
{% endslot %}
</div>
<aside>
{% slot "sidebar" required / %}
</aside>
"""
@register("parent")
class Parent(Component):
template = """
<div>
{% component "child" %}
{% fill "content" %}
๐๏ธ๐ฐ
{% endfill %}
{% fill "sidebar" %}
๐ท๐ง๐พ
{% endfill %}
{% endcomponent %}
</div>
"""
Slot dataยค
Any extra kwargs will be considered as slot data, and will be accessible in the {% fill %}
tag via fill's data
kwarg:
Read more about Slot data.
@register("child")
class Child(Component):
template = """
<div>
{# Passing data to the slot #}
{% slot "content" user=user %}
This is shown if not overriden!
{% endslot %}
</div>
"""
@register("parent")
class Parent(Component):
template = """
{# Parent can access the slot data #}
{% component "child" %}
{% fill "content" data="data" %}
<div class="wrapper-class">
{{ data.user }}
</div>
{% endfill %}
{% endcomponent %}
"""
Slot fallbackยค
The content between the {% slot %}..{% endslot %}
tags is the fallback content that will be rendered if no fill is given for the slot.
This fallback content can then be accessed from within the {% fill %}
tag using the fill's fallback
kwarg. This is useful if you need to wrap / prepend / append the original slot's content.