Components composition and attributes #755
-
Hi, I have a button and an icon component like in the example below. The button it's used in the template like this How is the right way to implement something like my use case in django-components? Thanks.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @vb8448, your approach is fine! But if it doesn't feel right, you can use the spread operator ( @register("button")
class Button(Component):
def get_context_data(
self,
value: Optional[str] = None,
html_type: Optional[str] = None,
klass: Optional[str] = None,
**kwargs
) -> dict:
icon_attrs = {k[2:]: kwargs.pop(k) for k in list(kwargs.keys()) if k.startswith("i_")}
return {
"icon_attrs": icon_attrs,
"base_attrs": {
"html_type": html_type or self.html_type,
"class": self.html_class,
},
}
# language=HTML
template: types.django_html = """
<button {% html_attrs base_attrs class=klass %} >
{% if icon_attrs %}
{% component "icon" ...icon_attrs / %}
{% endif %}
</button>
""" |
Beta Was this translation helpful? Give feedback.
Hi @vb8448, your approach is fine! But if it doesn't feel right, you can use the spread operator (
...dict
) to do the following :)