8 ms·
Writing a Jinja-inspired template library in Python
- danmur 5y agoI seem to be in the minority but I'm not a fan of jinja2. I like mako better, I guess you have more leeway to do silly things but I never feel like I can't do what I want.
- deleted 5y ago[deleted]
- memco 5y agoHaving never used Mako the appeal to me over Jinja is that it allows you to use real control structures. Jinja is a bit of a pain to debug and develop because it does some things slightly differently than you'd do in native Python so even though I can setup breakpoints and evaluate the application state I can't easily convert the code changes I make in the debugger back to the template. I get that there are reasons why Jinja and other templating systems guard against this, but it does have a cost.
- jxramos 5y agocan you detail the idea about real control structures, are you talking about if and elif and else clauses? Is there something partial about jinja's control structures? https://jinja.palletsprojects.com/en/3.0.x/templates/#list-of-control-structures https://jinja.palletsprojects.com/en/3.0.x/templates/#list-o...
- simonw 5y agoI've seen a large scale Mako codebase and the amount of business logic that had sneaked into the templates was horrifying - really made me double-down on my preference for limited template languages that help people avoid that temptation. I helped design Django's template language and I now think we took keeping it restrictive a bit too far though - these days I prefer working with Jinja because it has better support for complex expressions and lets you call functions within your templates. That does mean it requires a bit more discipline to use responsibly though, but being able to control which functions are exposed to the template context helps a lot there.
- wodenokoto 5y agoI hate jinja. I really don’t get it. So here’s a complaining reply, so you hopefully won’t feel as alone. When using jinja2 for website builders, it just feels like a poor version of old school php. I’ve also seen it used for generating json/yaml config files, which to me is absolutely bunkers compared to just building a dictionary in Python and exporting it.
- iamgopal 5y agoAre there other template library in other languages ( especially in rust or go ) that converts templates in to native language code for execution? Like the way jinja do.
- giancarlostoro 5y agoIn D, vibe.d does this. It uses a pug like syntax for templates. Edit: They've extracted the library into its own thing called diet-ng https://vibed.org/ https://vibed.org/ https://github.com/rejectedsoftware/diet-ng/ https://github.com/rejectedsoftware/diet-ng/
- denysvitali 5y agoYes, there is pongo2 [0] and my runner (basically a small Go software that runs the template engine) pongo2-runner [1]. I'm not the author of the library (pongo2), but I'm using pongo2-runner to dynamically create config files out of environment variables, with custom logic. Super recommended. [0]: https://github.com/flosch/pongo2 https://github.com/flosch/pongo2 [1]: https://github.com/swisscom/pongo2-runner https://github.com/swisscom/pongo2-runner
- eatonphil 5y agoPongo2 is a tree walking interpreter from everything I see. It does not compile the templates into bytecode or native code before executing/interpreting.
- denysvitali 5y agoOh yes, sorry, misread that. Never mind then!
- conradfr 5y agoTwig, inspired by Jinja2. https://twig.symfony.com/ https://twig.symfony.com/
- shakna 5y agoMost template languages for Lua generate functions, that will be called with their own environment. Having first-class environments makes that the "obvious" way of doing things. For example, here's a 160LOC one I wrote a while back. [0] [0] https://git.sr.ht/~shakna/lserver/tree/master/item/template.lua https://git.sr.ht/~shakna/lserver/tree/master/item/template....
- jxramos 5y agoI was always hoping to find something that would do partial renderings of a template before some exception occurred while rendering said template. This would be a great debug feature. https://stackoverflow.com/questions/56084066/is-there-a-way-for-jinja-to-output-its-partially-rendered-content-before-an-exce https://stackoverflow.com/questions/56084066/is-there-a-way-...