3 ms·
I feel like I should emphasize some of the text in my original question: 'Now suppose you need to allow the template user to pass in ANY attributes they want in
by yawaramin 1y ago
I feel like I should emphasize some of the text in my original question: 'Now suppose you need to allow the template user to pass in ANY attributes they want into the <div> tag, BUT you need to make sure that when it is finally rendered, one of its CSS classes MUST be `dropdown`.'
Fortunately, a Jinja guru in another channel showed me how to do it using a macro:
{% macro combo_box(class="") %}
<combo-box class="{{ class }} dropdown" {{ kwargs | xmlattr | safe }}>
{{- caller() if caller -}}
</combo-box>
{% endmacro %}
This pulls out the `class` attribute's value using the keyword argument and lets me render ALL the other passed-in attributes using the `kwargs` special variable. So, problem solved, with some abstraction, you're right.
BUT, for this `<combo-box>` custom element I also need to pass in multiple 'slots' ie children elements to inject inside various parts of the final rendered component. With HTML generation functions and values, this is relatively easy. With a template, it's possible but very unintuitive and ugly-looking, unfortunately!
- danpalmer 1y agoThis is very Jinja2 specific, but yes this is pretty much what I suggested.