template module

A small Jinja-flavored template engine. Supports {{ var }} interpolation, {% if %} / {% for %} / {% block %} / {% extends %} / {% include %}, a filter pipeline ({{ name | upper }}), and {%- ... -%} whitespace control.

import "template" as tpl;

tpl.set_views("./views");

var html = tpl.render("home.html", { user: "Alice", items: [1, 2, 3] });
print(html);

API

FunctionDescription
tpl.render(path, ctx) → stringRead <views_dir>/<path> and render.
tpl.render_string(text, ctx) → stringRender an in-memory template.
tpl.set_views(dir)Set the base directory used by render.
tpl.clear_cache()Drop compiled-template cache.
tpl.register_filter(name, fn)Add a filter. fn(value, ...args) → value.

Syntax cheat-sheet

{# comment #}
{{ user.name | upper }}
{% if items %}
  <ul>
    {% for item in items %}
      <li>{{ item }}</li>
    {% endfor %}
  </ul>
{% else %}
  No items.
{% endif %}

{% extends "base.html" %}
{% block content %}...{% endblock %}
{% include "_footer.html" %}

Built-in filters

upper, lower, length, default, escape (auto for {{ }}), safe (suppress escaping), join, replace.

See also

  • webweb.view(path, ctx) is a thin wrapper over tpl.render.