A typed template engine, subset of jinja2
Haiji is a template engine which is subset of jinja2. This is designed to free from the unintended rendering result by strictly typed variable interpolation.
{{ foo }}
For example, this jinja2 template requires "foo". A dictionary which provides a variable "foo" is required to render it. If a variable "foo" does not exist in a given dictionary, jinja2 evaluates it to an empty string, whereas haiji treats this case as compile error.
You can use a dot to access attributes of a variable.
{{ foo.bar }}
- abs
- length
{{ foo|length|abs }}
{% if foo %}foo{% elif bar %}bar{% else %}baz{% endif %}
{% for bar in foo %}
loop: {{ bar }}
{% endfor %}
{% for y in ys %}
{% set prev_loop = loop %}
{% for x in xs %}
{{ prev_loop.index0 }} {{ loop.index0 }}
{% endfor %}
{% endfor %}
Don't support
- immediate value assignment
- multiple targets
- namespace objects
- block assignments
{% include "parts.html" %}
Base template
<html>
<head>
{% block head %}
<title>{{ foo }}{% block title %}{% endblock %}</title>
{% endblock %}
</head>
<body>
{{ bar }}
{% block missing %}child missing this block{% endblock %}
</body>
</html>
Child template
{% extends "parent.tmpl" %}
{% block title %}{{ super() }}{{ baz }}{{ super() }}{% endblock %}
{% block head %}
{{ super() }}
<style type="text/css"></style>
{% endblock %}
Raw block
{% raw %}
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endraw %}
{# a comment #}
Integer Literals
{{ 123 }}
Boolean Literals
{{ true }}
{{ false }}
{{ foo + bar}}
{{ foo - bar}}
{{ foo * bar}}
{{ foo // bar}}
{{ foo % bar}}
{{ foo ** bar}}
{{ foo == bar }}
{{ foo != bar }}
{{ foo > bar }}
{{ foo >= bar }}
{{ foo < bar }}
{{ foo <= bar }}
{{ foo and bar }}
{{ foo or bar }}
- range
{% for i in range(n) %}{{ i }}{% endfor %}
The tests can be run with stack
:
$ stack test
In order to run the tests, you need a python2
binary on your PATH
, and the
jinja2
python library installed.