Filters are simply functions that perform transformations on their first input argument.
Filters are run at render time, so they may not directly modify the compiled template structure in any way.
All of Swig's built-in filters are written in this same way. For more examples, reference the `filters.js` file in Swig's source.
To disable auto-escaping on a custom filter, simply add a property to the filter method `safe = true;` and the output from this will not be escaped, no matter what the global settings are for Swig.
Name | Type | Optional | Default | Description |
---|---|---|---|---|
input | * |
undefined |
Input argument, automatically sent from Swig's built-in parser. |
|
args | * |
✔ | undefined |
All other arguments are defined by the Filter author. |
// This filter will return 'bazbop' if the idx on the input is not 'foobar'
swig.setFilter('foobar', function (input, idx) {
return input[idx] === 'foobar' ? input[idx] : 'bazbop';
});
// myvar = ['foo', 'bar', 'baz', 'bop'];
// => {{ myvar|foobar(3) }}
// Since myvar[3] !== 'foobar', we render:
// => bazbop
// This filter will disable auto-escaping on its output:
function bazbop (input) { return input; }
bazbop.safe = true;
swig.setFilter('bazbop', bazbop);
// => {{ "<p>"|bazbop }}
// => <p>