Evaluation and comparison of various Python templating libraries
Last updated: October 2023
- Either Django, Jinja2, Mako, or Cheetah seem like decent choices (though Cheetah feels more like a bet)
- I'd prob run Mako through some proof of concept work since it's closest to my preferences, then choose it
- If the proof of concept fails, then I'd try Jinja2 (which I think I've used before)
- Worst case is Django since I know that works (though not the friendliest)
- Alternatively, I could try using PyPugJS or Plim since those get me even more mileage on preferences and possibly better syntax highlighting support
- Escapes variables for HTML, with escape hatch for no escaping
- Inheritance system(s) (ideally both include style as well as extension style)
- Syntax highlighting well supported in IDEs
- Nice to have: Has escape tools for JS and JSON
- Nice to have: Linter and formatter support
- Preferred: Raises an error when a variable is not defined
- Preferred: Escape hatch to Python, since sometimes we to map variables, and bespoke DSLs are frustrating/limited
- Bonus: Can express multiple tags in 1 line (a la Jade/Pug)
strong > a
- Bonus: Can express ids and classes easily (a la Jade/Pug, HAML, Slim)
Name | Notes |
---|---|
Django | URL, (GitHub), Well established, only really available in Django, but has large headache around not raising errors for missing variables =/ (Django Docs) |
Jinja2 | URL
(GitHub),
Another well established Python library with plenty of good features (e.g. inheritance, Python Methods)
Can do multi-line Python if needed it seems, though quite verbose ._. (StackOverflow) |
Mako | URL (GitHub), Not nearly as many stars as others (286) but it's got recent development, is owned by SQLAlchemy, and Reddit uses it (as per homepage) so prob a safe bet Syntax
seems pretty straight forward (it's Python with a twist of The Python Block syntax seems like such a killer feature ._. It also has includes, and even early returns O_O (getting some PHP vibes right there)
So prob worth exploring further (and prob the front runner for me) There's a syntax highlighting package, https://packagecontrol.io/packages/Mako, though not touched in a while |
Chameleon | https://chameleon.readthedocs.io/en/latest/reference.html It's maintained but the syntax is to use |
Cheetah |
It's maintained (https://github.com/CheetahTemplate3/cheetah3) and some good support, though the website feels like it hasn't been touched in a while, which makes me concerned around how feature rich it is =/ There's a syntax highlighting package, https://packagecontrol.io/packages/Cheetah%20Syntax%20Highlighting, though not touched in a while Has good inheritance support, https://cheetahtemplate.org/users_guide/inheritanceEtc.html though it's def its own DSL, not just Python =/ (which means more learning/ramping up -- but that's prob fine) |
Genshi | https://github.com/edgewall/genshi Gets some maintenacne (e.g. last year) and has some stars (78) https://genshi.edgewall.org/wiki/Documentation docs seems kind of confusing I'm going to just sidestep this one ._. |
Spitfire | https://github.com/youtube/spitfire Good traction (390 stars) and 2019 commit, but docs not touched in a long while (GitHub) and the docs are much more absent than present =/ |
Kaijiki | https://github.com/jackrosenthal/kajiki 43 stars + last commit in Nov 2022 -- but... Copying Genshi and Jinja so not getting much new content and seems to lack many features, https://docs.kajiki.io/templating-basics.html#template-expressions-and-code-blocks |
PyPugJS | https://github.com/kakulukia/pypugjs 246 stars O_O + last commit Aug 2023! Transpiles from Pug to relevant language (e.g. Django, Jinja, Mako, Tornado) and there's a loader for Django to avoid manually transpiling every time Does seem quite nice and expressive, but also guessing solo maintainer and iffy longevity =/ (previous fork it's based on got archived before) |
Tornado | https://www.tornadoweb.org/en/stable/template.html Some pretty good features (though no better than Django or Jinja2) but stuck inside Tornado framework |
Plim | https://github.com/avanov/Plim Slim port for Python =D 2022 and 173 stars! Built on top of Mako, not digging much deeper since it's not independent ._. |
Not enough traction |
|
Too lightweight |
|
Not maintained |
|
- Starting with known templates
- As well as criteria
- [x] General Python templates
- [x] "python templating"
- [x] https://wiki.python.org/moin/Templating
- [x] https://opensource.com/resources/python/template-libraries
- [x] Tempy
- [x] Kajiki
- [x] https://www.fullstackpython.com/template-engines.html
- [x] Diazo - Not a templating engine, something about themes? https://docs.diazo.org/en/latest/
- [x] Juno
- [x] pystache
- [x] HAML variations
- [x] "haml python"
- [x] Jade variations
- [x] "jade python"
- [x] Pug variations
- [x] "pug python"
- [x] Slim variations
- [x] "slim python" (nothing)
- [x] "slim python template" (something)
Fwiw, in my experience the "this templating language ported to your language" almost never lasts as a supported library =/
(unless it's something intentionally universal like Mustache)
So usually a pretty bad idea to build on top of ._.
RST is required for multiline behavior in tables. Markdown doesn't support this =(
I suggest reconsidering EmPy. After 4 years it was now updated and released a new version 4.0, which makes it maintained.