Templating

Acetate uses the Nunjucks templating language for all its templating. Nunjucks is powerful and has all the features you would want to build a complex website.

Basic Nunjucks

In general you should refer to the Nunjucks templating documentation but a quick introduction to the basics will be helpful.

Printing Variables

You can print a variable in Nunjucks by wrapping it in double curly braces like {{ variable }}.

Unlike other templating libraries Nunjucks does not autoescape content by default. But you can use the escape filter{{ variable | escape }}.

If you need to render Nunjucks syntax You can use a raw tag. See the Nunjucks documentation for details.

Iterating Over Arrays

You can use Nunjucks {% for %} tag to iterate over Arrays.

src/iterating-arrays.html

---
items:
  - title: Foo
    id: 1
  - title: Bar
    id: 2
---

<ul>
{% for item in items %}
  <li>{{ item.title }}</li>
{% endfor %}
</ul>

Iterating Over Objects

You can use Nunjucks {% for %} tag to iterate over Objects.

src/Iterating-objects.html

---
dogs:
  Spot: Lab
  Rover: Corgi
---

{% for name, breed in dogs %}
  {{ name }} is a {{ breed }}
{% endfor %}

Conditionals

You can use Nunjucks' {% if %} tag to create conditional statements.

src/conditionals.html

{% if src == 'index.html' %}
  On the home page.
{% elif src == 'about.html' %}
  On the about page.
{% else %}
  On some other page.
{% endif %}

Expressions

You can use most JavaScript literals like Strings, Numbers, Arrays, Objects, Regular Expression and functions via Nunjucks expressions

Layouts

You can use either the Acetate layout system or Nunjucks template inheritence to create reuseable layouts to wrap your content.

Partials

Acetate leverages the Nunjucks include tag to create partials that can be used across multiple pages. To learn how to use partials in Acetate refer to the partials documentation.

Helpers

In addition to having access to everything in the Nunjucks templating language you can also access built in variables provided by Acetate and create custom helpers to use in your template.

Filters

To use a filter add it after a variable separated by a | character {{variable | filter}}.

You can use any of the Nunjucks filters like sort, first, and trim and you can define your own filters.


Edit this Page on GitHub