gist-python-templating-evaluation ================================= .. Workaround for occasional headaches from RST (e.g. inline links needing unique names) .. role:: raw-html(raw) :format: html An evaluation and comparison of various Python templating libraries Last updated: October 2023 Criteria -------- - 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 - `Django templates don't do this `_ (:raw-html:`StackOverflow`) - 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) .. _`Jade/Pug`: https://pugjs.org/api/getting-started.html .. _`HAML`: https://haml.info/ .. _`Slim`: https://slim-template.github.io/ Comparison ---------- +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Name | Notes | +=====================+===============================================================================================================================================+ | Django | :raw-html:`URL`, | | | (:raw-html:`GitHub`), | | | Well established, only really available in Django, | | | but has large headache around not raising errors for missing variables =/ | | | (:raw-html:`Django Docs`) | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Jinja2 | :raw-html:`URL` | | | (:raw-html:`GitHub`), | | | Another well established Python library with plenty of good features (e.g. inheritance, Python Methods) | | | :raw-html:`
` | | | Can do multi-line Python if needed it seems, though quite verbose ._. | | | (:raw-html:`StackOverflow`) | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Mako | :raw-html:`URL` | | | (:raw-html:`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 | | | | | | | | | :raw-html:`Syntax` | | | seems pretty straight forward (it's Python with a twist of ``endfor`` and such) | | | and some of the most expressive/sanest (though a little too unguarded) experiences I've had | | | have been in EJS and ERB | | | | | | | | | 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) | | | :raw-html:`
` | | | And covers inheritance, https://docs.makotemplates.org/en/latest/inheritance.html | | | | | | 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 ``
`` to include/exclude an element, | | | not straightforward expression and I'd be wary of footguns from it =/ | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Cheetah | https://cheetahtemplate.org/ | | | | | | 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 | | | (:raw-html:`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 | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Not enough traction | - https://github.com/tetframework/Tonnikala, Some life (2022 update) but only 14 stars | | | - https://github.com/cesbit/trender, 2022 update but only 20 stars | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Too lightweight | - https://docs.python.org/3.4/library/string.html#template-strings | | | - https://www.stringtemplate.org/ | | | - Mustache (no inheritance and such) | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ | Not maintained | - https://github.com/mll/ctemplate-python (last touched 2015) | | | - http://airspeed.pythonconsulting.com/ (site down) | | | - https://pypi.org/project/cubictemp/#history (last touched 2013) | | | - https://www.nthwave.net/elements/ (site down) | | | - http://www.alcyone.com/software/empy/ (version is from 2004) | | | - https://pypi.org/project/evoque/#history (last release 2009) | | | - https://blog.aerojockey.com/post/hrl (dropped for Mako in 2010) | | | - https://github.com/mahmoud/ashes#ashes (last touched 4 years ago) | | | - https://github.com/etianen/moody-templates (2013) | | | - https://pypi.org/project/Myghty/#history (2010) | | | - Qpy: https://www.mems-exchange.org/software/DurusWorks/ (2013) | | | - https://github.com/avelino/quik (2013) | | | - https://codingrecipes.com/pml-a-python-template-engine (timeout) | | | - https://www.simple-is-better.org/template/pyratemp.html (2013) | | | - https://wiki.python.org/moin/Spyce (2006) | | | - https://pypi.org/project/suit/#history (2010) | | | - https://pythonpaste.org/tempita (spam site) | | | - http://tt2.org/python/index.html (SVN?! doubt maintained) | | | - http://davidbau.com/archives/2011/09/09/python_templating_with_stringfunction.html (2011) | | | - Templite+: https://git.joonis.de/snippets/4 (snippet, not a project) | | | - https://www.kuwata-lab.com/tenjin/ (SSL error) | | | - http://texthon.chipsforbrain.org/ (2013) | | | - thrases: https://sourceforge.net/projects/the-next-please/ (2013) | | | - https://bitbucket.org/akorn/wheezy.template (repo gone + stated as lightweight) | | | - https://github.com/Hrabal/TemPy (says not maintained) | +---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------+ Running eval notes ------------------ 2023-10-16 ^^^^^^^^^^ - Starting with known templates - As well as criteria - [ ] 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 - [ ] https://www.fullstackpython.com/template-engines.html - [ ] Diazo - [ ] Juno - [ ] pyratemp - [ ] pystache - [ ] HAML variations - [ ] Jade variations - [ ] Pug variations - [ ] Slim variations 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 ._. Why is this document in reStructuredText? ----------------------------------------- RST is required for multiline behavior in tables. Markdown doesn't support this =(