Filters
To pass options to the filter, add them inside parentheses after the filter name (just as you would do with tag attributes): .
All can be used as Pug filters. Popular filters include :babel
, :uglify-js
, :scss
, and :markdown-it
. Check out the documentation for the JSTransformer for the options supported for the specific filter.
If you can’t find an appropriate filter for your use case, you can write your own custom filter.
For example, if you want to be able to use CoffeeScript and Markdown (using Markdown-it renderer) in your Pug template, you would first make sure that these features are installed:
:markdown-it(linkify langPrefix='highlight-')
# Markdown
Markdown document with http://links.com and
```js
var codeBlocks;
```
:coffee-script
<h1>Markdown</h1>
<p>Markdown document with <a href="http://links.com">http://links.com</a> and</p>
<pre><code class="highlight-js">var codeBlocks;
</code></pre>
<script>
(function() {
console.log('This is coffee script');
}).call(this);
</script>
Warning
Filters are rendered at compile time. This makes them fast, but it also means that they cannot support dynamic content or options.
By default, compilation in the browser does not have access to JSTransformer-based filters, unless the JSTransformer modules are explicitly packed and made available through a CommonJS platform (such as Browserify or Webpack). In fact, the page you are reading right now uses Browserify to make the filters available in the browser.
Templates pre-compiled on the server do not have this limitation.
If the content of the filter is short, one can even use filters as if they are tags:
<p><strong>BOLD TEXT</strong></p>
</p>
You can apply multiple filters on the same block of text. To do so, simply specify the filters on the same line.
The filters are applied in reverse order. The text is first passed to the last filter; then, the result is passed to the second last filter, and so on.
In the following example, the script is first transformed by babel
, and then by .
script
:cdata-js:babel(presets=['es2015'])
const myFunc = () => `This is ES2015 in a CD${'ATA'}`;
You can add your own filters to Pug via the filters
option.
options.filters = {
'my-own-filter': function (text, options) {
if (options.addStart) text = 'Start\n' + text;
if (options.addEnd) text = text + '\nEnd';
return text;
}
};
p
:my-own-filter(addStart addEnd)