Custom Helpers

Custom Filters

You can configure custom filters for Nunjucks with the acetate.filter helper. The first parameter is the name of your filter. The second is the function that will process the variable and return it.

acetate.filter('prettyJSON', function(str){
  return "<pre>" + JSON.stringify(JSON.parse(str),null, '  ') + "</pre>";
});

Once your filters are defined you can use them in your templates.

src/json-example.html

---
json: "{\"foo\":\"bar\", \"baz\":\"bar\"}"
---

{{ json | prettyJSON }}

Refer to the Nunjucks filter documentation for more details.

Helper Functions

You can use the helper function in your config file to define custom helpers you can use in your templates. helper takes 3 parameters, a tag name which you will use in your templates, a handler function and a default set of options. The handler function will be passed the page context (all the variables on the page) and then any arguments you pass from the template.

acetate.conf.js


acetate.helper('custom_helper', function helperHandler (context, param){
  // you can access the metadata from the current page in `context.page`
  // you can get your options in `context.options`
  return arg; // return the output of your helper
}, {
  option: ''
});

You can now use your helper in your pages. You can override your default options with option="value" all other parameters are passed to your handler function directly.

src/blog/index.html

{% custom_helper 'foo', option='bar' %}

Block Helpers

You can also create special helpers which can receive blocks of content by using the acetate.block function. ``block` takes 3 parameters, a tag name which you will use in your templates, a handler function and a default set of options. The handler function will be passed the page context (all the variables on the page) then the body of your block, and finally any arguments you pass from the template.

acetate.conf.js

acetate.block('custom_block', function(context, body, param){
  return body;
}, {
  option: ''
});

Now that your helper is registered you can use it in your templates.

src/blog/index.html

{% custom_block 'foo' %}
I'm in a custom block!
{% endcustom_block %}

Edit this Page on GitHub