4 ms·
MiniJinja: Learnings from Building a Template Engine in Rust
- infogulch 2y agoA templating language would be a great case for using an arena allocator since objects will only live as long as it takes to fulfill the request.
- the_mitsuhiko 2y agoYes! I did in fact try this but I was unable to find a good way to make the API work.
- infogulch 2y agoInteresting! I suspect a templating engine that uses arenas would be a holy grail for rust web perf, so I'm curious about your experience trying to use them. As for making the API work, do you mean that the arena API was difficult to use, or the object interface you built on top of it was difficult to implement in a way that was accessible to template execution?
- the_mitsuhiko 2y agoIt depends a bit on what you want to do. I made one attempt where I needed to carry a 'arena lifetime everywhere which was really tricky. A second attempt was to tag the Value internally with an ever increasing ID of arenas. However the latter becomes dangerous very quickly because nothing binds them together and you end up with a lot of unsafe code and I had no trust in my creation.
- infogulch 2y agoYeah that makes sense. Maybe arenas will be viable once the working group makes progress on allocators. https://github.com/rust-lang/wg-allocators https://github.com/rust-lang/wg-allocators
- HiHelloBolke 2y agoThank you for this, I just used this to create a rust based cli tool to create j2 yamls. This was motivated because we want to not use ansible j2 yaml generation instead to a standalone binary for it. Now it feel like helm but it’s j2 and outputs a directory of yamls with base and overlays in kustomization way. So they can be iverriden in gitops by sre if needed
- brucecrust 2y agoha man I hate Jinja. Nothing against this or Rust, just from working in Flask I have borderline nightmares. That might say more about my team that the framework though.
- 0cf8612b2e1e 2y agoCould you elaborate? I think it’s fine? Although I have always subscribed to the “Two Scoops” philosophy and keep my templates as stupid as possible. Use a variable, simple conditions, etc. Anything more complex should be handled at a higher level. Go templating on the other hand, that drove me bananas.
- murkt 2y agoI got used to React components, and after that templates feel a bit cumbersome and un-ergonomic in places. I’ve been using Django templates since 2006. Jinja was designed after Django templates of that era, fixing their most glaring problems. Don’t remember the exact year when I started using Jinja a lot. Maybe 2013. For example, a pure existence of an {% include %} tag. People use it, implicitly passing many arguments to some other template. Which makes it very hard to understand and change later. Macros are not that pleasant to use, there are less features in macros then in Python functions, and tools support is much worse too (static analysis, formatting, etc). Filters… why do I need to write `value|string` instead of `str(value)`? Etc.
- the_mitsuhiko 2y agoThe benefit of filters is that they don’t share the namespace with the template context and I would argue are more readable.
- murkt 2y agoNot sharing the same namespace is a benefit, I agree. Not a huge one, as I don’t name my variables “map”, “string” and similar anyway. And would not look favorably if any of my employees did that.
- VWWHFSfQ 2y agoI wanted to make a cli tool for rendering templates with mini jinja and also for users to write their own little library of functions/filters etc in Lua using mlua and then be able to use those in their templates. Unfortunately didn't work. All kinds of issues with mini jinja and mlua interactions when trying to add_filter, add_function, etc. This was awhile ago though. Maybe something changed and I should try it again
- the_mitsuhiko 2y agoYou cannot register filters with a non 'static lifetime. However it’s not super hard to work around. Usually an Arc<Mutex> is all you need.
- ctoth 2y ago> The first thing is that Rust is just not particularly amazing at tree structures. As someone who doesn't write any Rust I'd like to understand this more. Is it the ownership model that makes trees weird? Is there something you guys do instead? I was working on an R-tree yesterday for something and I'm slightly surprised to read this but obviously Armin knows what he's talking about :)
- __s 2y agoit's a pain if you want children to reference their parents while their parents reference them Similarly getting a mutable reference to a leaf while parents have references to children is awkward
- giancarlostoro 2y agoI know just Rust basics, what specifically makes it awkward? I'm not a heavy Rust user, so I'm having a hard time following, do you have any simple pseudo-Rust code you could share? I'm also a basic CRUD app dev by day, hobbyist by night.
- BonusPlay 2y agoI'm not a rust expert by any means, but I believe there's a problem with lifetimes. There are many ways to implement double-linked lists (think C++ smart pointers), but when you try to squeeze performance and use references, then it's a fight against borrow checker, which ends with the need to use unsafe rust. smarter sources: https://rust-unofficial.github.io/too-many-lists/ https://rust-unofficial.github.io/too-many-lists/ https://softsilverwind.github.io/rust/2019/01/11/Rust-Linked-Lists.html https://softsilverwind.github.io/rust/2019/01/11/Rust-Linked...
- oneshtein 2y agoBorrow checker is just a checker, which ensures that programmer doesn't write obviously wrong programs. It's obviously wrong to create cyclic references, even in a language with garbage collector, such as JavaScript. Why you need to fight with checker?
- pietz 2y agoI really don't like Jinja. The syntax is not for me and there's often three different ways of building the same thing. I find it a bit confusing. Recently I started using JinjaX (https://jinjax.scaletti.dev/ https://jinjax.scaletti.dev/), which is weirdly amazing. I think the comparison on the homepage explains it quite well. It feels like what Jinja always should have been and and the multiple ways of doing the same thing converge into one syntax in JinjaX. I really hope the project gains more traction.
- murkt 2y ago> JinjaX components are simple Jinja templates. You use them as if they were HTML tags without having to import them: easy to use and easy to read. …and impossible to understand and keep track of what goes where. No, thanks. Components are a good idea, and no imports is a horrible idea.
- adontz 2y agoDo I understand JinjaX correctly, that it's specifically for HTML templates, not text templates in general?
- pietz 2y agoYes
- krick 2y agoIDK. I always though Jinja is the best templating engine and was surprised by your complaints and the Jinja sample on that website. What doesn't he like? Looks totally legit... After reading JinjaX sample, I see the point. I would agree that it's much cleaner... If not for the fact he uses capitalized tags to distinguish between templates and html. I hate it. First off, it just throws me off, I cannot tell at the glance anymore where is the real text, and where is templating voodoo-magic. How do I find the source files fot layout and pagination? Right, I just have to know the convention. Second, it's remotely justifiable and can be attributed to a matter of getting used to only if it produces html. Templating engines are not restricted to html. It could be producing markdown, it could be producing XML. If only the author of this library restrained himself to be less funky and not introduce any ambiguous idioms, it would be pretty perfect. But as it is, I don't think I could use it.
- geenat 2y agoIMHO Jinja was never worth salvaging. It's a bandaid for not being able to mix Python and HTML easily. Super slow parsing. Unintuitive DSL (opinionated enough to be a PITA). Huge LoC vs the value it contributes to your project. Adopt a different system.. see: ERB, PHP, EJS- all of these are extremely fast and intuitive (just the native language + an extra tag or two). Edit: Of course.. this is authored by the jinja guy.
- krick 2y agoExcept no cultured adult programmer would use the original PHP syntax for templating, unless it's some 5-minutes worth development script. It's too powerful and too complicated. People use smarty or twig, which are basically the same thing as Jinja.
- lyu07282 2y agoI always liked TAL, it kind of made sense to me that the templating engine itself understands the syntax of the language you are templating. It ought to be impossible to write invalid HTML/XML. It's like the pain you get into when templating YAML with jinja, but if the YAML template also were itself a valid YAML file it would be much cleaner imho.
- the_mitsuhiko 2y agoI don't disagree but we ended up with systems like Jinja because that's what people adopted. I also built GHRML which was a syntax aware template engine for generating XHTML documents and it never really took off. Sometimes it's the simple and dumb solutions that win out over the cleaner stuff. Nowadays maybe less so, JSX clearly won and if you build UIs today that's probably what you should go for. However there were many solutions like JSX a long time before and somehow the value proposition was not right for the software written at the time.
- Jackevansevo 2y agoTo add some balance to everyone slating Jinja in the comments, I've personally found it great to use. Sure you CAN write unmaintainable business logic spaghetti in your templates, doesn't mean you SHOULD (Most criticism appears to come from this angle).