Zen: filters

Back to Zen homepage

prev next

1. the if filter

In template system terminology, filters are a way to create new functions in template system syntax. In Zen there are only one two built-in filters: html mentioned earlier and if mentioned below. Here the comma is echoed everywhere but the last position in the range.
{loop(range(1,10) as $n):} {=$n}{=if:!$n->isLast,","} {/loop}

Result

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2. custom filter with more parameters

You can create your own from PHP function prefixed zen_. Note in the sample below that filters can have more than one parameter (comma separated).
<? function zen_shorten($s, $len) { return mb_substr($s, 0, $len); } ?> {=shorten: "hello, world", 4}

Result

hell

3. the translation filter

There is a standard tool gettext with shorthand function _() for translations, however {=_("text")} is not easy to read when all of the site text is translated. To maximize the code readability, nameless filter was created for this purpose: {:"text"}, quotes or apostrophes can be ommited, so {:text} is also possible. To utilize gettext, PO source translation files must be compiled to MO files and placed to defined folder with LC_ALL set. Pick any of the following syntax that you find appropriate in your source code.
{=_("Test string")} {=:"Test string"} {:"Test string"} {:Test string}

Result

Test translation
Test translation
Test translation
Test translation