So there is no type safety or IDE help if you pass an invalid context from python to be rendered, i.e. that doesn't match var names in the template
So... I find these "html in python" libs kind of ugly in the Python code side but I definitely see the advantage
And then the idea that you can build 'components' as python functions
They are basically doing JSX but without benefit of the custom syntax
Eg, suppose the user passes in the following attributes to the div:
<div id=foo style=color:red class=my-component>
You need to make sure that the final rendered version is: <div id=foo style=color:red class="my-component dropdown">
How do you do that?I'm most familiar with Django templates and Jinja2, and both of those provide template functions, filters, and/or various forms of control through which you can build up these sorts of abstractions.
I'd probably define a partial template for this dropdown as something like..
<div id="{{id}}" style="color:{{colour}}" class="{{classes}} dropdown">
{{content}}
</div>
If you wanted more control over, say, the colours, you could just use an enum in your language of choice and provide a rendering function that turns it into the string `color:{{colour}};`, so that you retain some safety. You could do similar things with classes perhaps, keeping that as a list of strings and enforcing no whitespace or other separators in the text classes, then rendering to a whitespace separated string.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!