misai

misai is a simple template engine for the Python programming language. It’s loosely inspired by Jinja and mustache. Sample code is shown below:

{{ #if logged_in }}
    {{ #for user : users }}
        {{ "Hello: " | concat: user.name }}
    {{ #end }}
{{ #end }}

“misai” means moustache in Malay, due to heavy use of curly braces.

misai features:

  • loops: {{ #for i : items }}{{i}}{{ #end }}
  • conditionals: {{ #if a }} a {{# elif b }} b {{ #else }} c {{ #end }}
  • variable assignment: {{ #set i = 123 }}
  • filters: {{ title | capitalize }}
  • comparison operators (==, !=, <=, >=, <, >): {{ a > b }}
  • logical operators (and, or): {{ red or (green and solid) }}
  • variables: {{ foo }} {{ foo.bar }} {{ foo["bar"] }}
  • literals (floats, integers, strings): {{ "str" }} {{ 42 }} {{ 3.14 }}

The project is under 500 LOC, and comes with no external dependencies. It implements a context sensitive lexer and a one-pass compiler that transforms template to Python bytecode internally.

misai is currently used to render a web version of 101 Zen Stories, a collection of stories compiled by Nyogen Senzaku in the early 20th century.

Source code: https://github.com/nkanaev/misai