Skip to content

Add filter/endfilter keyword support, center and indent functions#336

Open
navrocky wants to merge 1 commit into
pantor:mainfrom
navrocky:feature/filters
Open

Add filter/endfilter keyword support, center and indent functions#336
navrocky wants to merge 1 commit into
pantor:mainfrom
navrocky:feature/filters

Conversation

@navrocky

@navrocky navrocky commented May 28, 2026

Copy link
Copy Markdown
Contributor

This MR extends Inja's template language with Jinja2-inspired filter feature:

  1. Filter blocks — {% filter ... %} ... {% endfilter %}

Renders the block body to a string and passes it as the first argument to the filter expression. Any function — built-in or a user add_callback — that takes a string and returns a string works as a filter, and filters
can be chained with | and take extra arguments.

render("{% filter upper %}Hello {{ neighbour }}!{% endfilter %}", data); // "HELLO PETER!"
render("{% filter replace(\"e\", \"3\") | upper %}{{ neighbour }}{% endfilter %}", data); // "P3T3R"
render("{% filter center(11) %}name{% endfilter %}", data); // "    name   "

Filter bodies may contain {% if %}/{% for %} and other filter blocks (nesting supported).

  1. New helper functions: center and indent
  • center(value, width=80) — Python str.center semantics.
  • indent(value, width=4, first=false, blank=false) — width accepts an int (number of spaces) or a string used verbatim as the prefix, per the Jinja2 docs. The first line and blank lines are skipped unless first / blank
    are set.
render("{{ center(neighbour, 11) }}", data);          // "   Peter   "
render("{{ indent(\"line1\\nline2\", 4) }}", data);    // "line1\n    line2"
render("{{ indent(\"line1\\nline2\", \"> \") }}", data); // "line1\n> line2"

Both work in call form and pipe form ({{ x | center(11) }}).

Closes #328

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for "filter/endfilter" section

1 participant