5 ms·
Why use this over a templating language? I've always found it much easier to write basically-just-html with a few placeholders, and then render in Python or wha
by danpalmer 1y ago
Why use this over a templating language? I've always found it much easier to write basically-just-html with a few placeholders, and then render in Python or whatever else.
- collyw 1y agoFor some reason simplicity is out of fashion these days.
- nodesocket 1y agoAgree, I’m building a Flask based app and find using templates works well. Sure I have to know tailwind classes, but it’s not that hard to grok and putting the view layer into python seems like an unnecessary combination.
- erikgaas 1y agoTemplating is great. I think I prefer fasthtml because I can apply all of the python tricks I know. Python arguments seem to work very well. Having a list of dom elements and splatting them into another component would feel very similar to templating.
- anentropic 1y agoI find the thing that is awkward with templates in a large project is there is only an implicit link between the template and the code that populates it 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
- yawaramin 1y agoSuppose you need a template that renders some component inside a <div> tag. 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`. 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?
- danpalmer 1y agoWith abstraction, like in anything else. 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.
- yawaramin 1y agoI 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.