Why Jinja and Hyde don’t go together

Code and wisdom in this article have not been kept up-to-date. Use them at your own peril.

I have been working on converting my blog from dynamic blog software (namely, WordPress) to static blog software (namely, Hyde).

I chose Hyde because it seems to be the most mature of the Python static blog generators — on the assumption that sooner or later I will run into some corner where I think I can help improve the software, I’d just as soon work in Python than in any other common language. And what do you know, I already found such a corner.

Right now, Hyde supports only one templating engine, Jinja2. Unfortunately, Jinja2 is not particularly well-matched to Hyde.

Specifically, Jinja is limited in what Python code it can execute during template evaluation. It provides no access to arbitrary Python modules, and lacks many common Python language constructs. This makes perfect sense for a template engine designed to run in a dynamic template environment; limiting access to arbitrary Python modules limits security exposure of the code running on a server, potentially accepting malicious input.

On the other hand, Hyde is a static blog generator. It doesn’t run on a server, and it doesn’t accept malicious input. As a result, its flexibility is constrained (severely, in my opinion) by the limitations of Jinja.

Here are examples of some things I tried doing with Hyde and Jinja to which I could find no solution short of adding features to either Hyde or Jinja:

  • Given a set of blog posts, iterate over all the ones published in a given time period. (Seems like this would be common for a two-level blog archive page — for example, a yearly archive grouped by month.)
  • Generate a full-content RSS feed. To be fair, Hyde has a feature in its Jinja wrapper — markers — which allows me to do this if I edit to every blog post to put a marker at the beginning and the end. However, the fact that I can’t do this automatically is entirely due to the fact that I can’t call arbitrary Python code inside a template.
  • Include the time at which the website was generated on a page of the website (to automatically generate an up-to-date copyright statement).

At any rate, while I now have brought this website under Hyde and Jinja, I think my next step is to change Hyde to replace its template engine. Genshi is the one I am leaning towards, because it includes support for arbitrary Python code, and because I am already familiar with it from my work with Trac.

Filed under: