Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Merge branch 'filters-docs-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Jan 29, 2013
2 parents 05dfa80 + 61402db commit afb23c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions docs/docs/index.html
Expand Up @@ -244,9 +244,27 @@ <h1 id="filters-custom">Custom Filters</h1>

<p>In your <code>myfilters.js</code> file, each filter method is just a simple javascript method. For example, here's a filter to make a variable all lowercase:</p>

<pre><code data-language="javascript">exports.lower = function (input) {
return input.toString().toLowerCase();
<pre><code data-language="javascript">exports.myfilter = function (input) {
return input.toString().split('').reverse().join('');
};</code></pre>

<p>Once your filter has been imported, you can use it like any other, as a direct variable filter, or a tag-level filter:</p>

<pre><code data-language="swig">{% raw %}{{ name|myfilter }}{% endraw %}</code></pre>

<pre><code data-language="swig">{% raw %}{% filter myfilter %}I shall be filtered{% endfilter %}{% endraw %}</code></pre>

<p>You can pass arguments to your custom filters as well:</p>

<pre><code data-language="javascript">exports.prefix = function(input, prefix) {
return prefix.toString() + input.toString();
};</code></pre>

<pre><code data-language="swig">{% raw %}{{ name|prefix('my prefix') }}{% endraw %}</code></pre>

<pre><code data-language="swig">{% raw %}{% filter prefix 'my prefix' %}I will be prefixed with "my prefix".{% endfilter %}
{% filter prefix foo %}I will be prefixed with the value stored to `foo`.{% endfilter %}{% endraw %}</code></pre>

</article>
</section>

Expand Down
5 changes: 5 additions & 0 deletions tests/node/tags/filter.test.js
Expand Up @@ -22,6 +22,11 @@ describe('Tag: filter', function () {
expect(tpl({})).to.equal('hi! my name is paul!');
});

it('can accept dynamic arguments for the filter', function () {
var tpl = swig.compile('{% filter replace replaced by "g" %}hi. my name is paul.{% endfilter %}');
expect(tpl({ replaced: '\\.', by: "!"})).to.equal('hi! my name is paul!');
});

it('throws if filter does not exist', function () {
expect(function () { swig.compile('{% filter foobar %}{% endfilter %}'); })
.to.throwException();
Expand Down

0 comments on commit afb23c9

Please sign in to comment.