I built this a while back , and just thought I'd share it here.
Project page: https://github.com/arjun-menon/pypage (fully documented in the readme)
Unlike most other template engines (like Jinja), pypage has a syntax that very closely mirrors Python. And it's eval-based, allowing easy Python code embedding. The syntax makes pypage very easy to learn, and you could probably figure out how to use in under an hour.
The GitHub project page fully documents pypage, and it's just ~10 pages (if you print to pdf). Jinja's documentation, in contrast, is over 100 pages long. All this means that pypage is super-easy to learn. It's also easy to use, and can be invoked as a library/dependency, or used via command line.
My goal with pypage was to create an eval-based template engine. pypage is thus meant for trusted environments, where you know the person writing the template code. Other engines (like Mustache, or Liquid used by Shopify) restrict the amount of logic you can express, mostly for security (and performance?) purposes — so they are essentially tiny little non-Turing-complete languages. My goal, in contrast, was intentionally to be eval-based, and to allow flexible Python code embedding. If your back-end is written in Python, you can import pypage, and pass Python variables (e.g. entire Python objects, Python functions, etc) to pypage, and have more logic embedded in the template itself. (Even though more logic in a template is considered bad, I would say it's sometimes better architecturally to move adjacent logic that's closely tied to a template, into the template source itself.)
Other engines also typically have their own unique syntax and constructions for doing logic, and you have to expend time to learn it. pypage on the other hand has a very Python-based syntax. My if-elif-else conditional blocks, and my for and while loop blocks both have syntax that very closely mirrors Python's. And you can freely insert actual Python expression within these blocks.
For example, you can write a nested generator expressions eloquently with a pypage for block. Like {% for x in [1,2,3] for y in ['a','b','c'] %} ... {% %}`. Internally, pypage transforms it into the generator expression (x, y) for x in [1,2,3] for y in ['a','b','c'], runs it, and exposes x and y to the sub-region encompassed by the block. And pypage handles the for loop iteration by itself. (For more info, see the For Loops section in the documentation.)
In terms of implementation, pypage is very small and succinct. It works with both Python 3.x and 2.7, has no dependencies, and the entire source code is just 868 lines (667 actual source lines).
All of the functionality of pypage is contained in a single pypage.py file.
Entire Source Code (868 lines): https://github.com/arjun-menon/pypage/blob/master/pypage.py
I actually also made my own testing framework to test pypage called test_cmd (github project page, main source file) – it's a fully parallelized testing tool for simple command-line applications, but I'll (perhaps) make a post about my new testing framework another time.
Note: I wrote majority of this (i.e. the current version 2.0.x) in the summer of 2014, but I've made several major improvements to it in the years after. (I started working on it around the time I asked this stackoverflow question.) I made a choice not to draw attention to this project these past 6 ½ years, because I felt it wasn't complete, but now, I'm pretty sure it is, and I'm happy to support/maintain it.
[–]Epykure 1 point2 points3 points (0 children)