Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active April 29, 2024 01:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/b861c182107cefcef086266c3b4b83a6 to your computer and use it in GitHub Desktop.
Save twolfson/b861c182107cefcef086266c3b4b83a6 to your computer and use it in GitHub Desktop.
Evaluation and comparison of various Python templating libraries

gist-python-templating-evaluation

Evaluation and comparison of various Python templating libraries

Last updated: October 2023

Conclusion and recommendation

  • 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

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
  • 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)

Comparison

Name Notes
Django , (), Well established, only really available in Django, but has large headache around not raising errors for missing variables =/ ()
Jinja2 (), 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 ._. ()
Mako

(), 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

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) 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 <div tal:condition="comments"> 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 () 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

Running eval notes

2023-10-16

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 =(

@blubberdiblub
Copy link

I suggest reconsidering EmPy. After 4 years it was now updated and released a new version 4.0, which makes it maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment