Skip to content

@getkist/action-nunjucks

Nunjucks template rendering with data support.

Installation

bash
npm install --save-dev @getkist/action-nunjucks

Actions

TemplateRenderAction

Renders Nunjucks templates to HTML or other formats.

Options

OptionTypeDefaultDescription
inputFilestringRequiredPath to template file
outputFilestringRequiredPath to output file
dataobject{}Data to pass to template
dataFilestring-JSON/YAML file with data
templatesDirstring-Directory for includes
autoescapebooleantrueHTML auto-escaping

Basic Usage

yaml
plugins:
  - @getkist/action-nunjucks

pipeline:
  build:
    stages:
      - name: html
        steps:
          - action: TemplateRenderAction
            options:
              inputFile: src/templates/index.njk
              outputFile: dist/index.html

With Inline Data

yaml
- action: TemplateRenderAction
  options:
    inputFile: src/templates/index.njk
    outputFile: dist/index.html
    data:
      title: "My Website"
      version: "1.0.0"
      features:
        - Fast
        - Modern
        - Simple

With Data File

Create data.json:

json
{
  "title": "My Website",
  "navigation": [
    { "name": "Home", "url": "/" },
    { "name": "About", "url": "/about" }
  ]
}

Use it:

yaml
- action: TemplateRenderAction
  options:
    inputFile: src/templates/index.njk
    outputFile: dist/index.html
    dataFile: src/data/site.json

Template Syntax

Variables

html
<title>{{ title }}</title>
<p>Welcome to {{ site.name }}</p>

Loops

html
<nav>
  {% for item in navigation %}
    <a href="{{ item.url }}">{{ item.name }}</a>
  {% endfor %}
</nav>

Conditionals

html
{% if user.isLoggedIn %}
  <p>Welcome, {{ user.name }}!</p>
{% else %}
  <a href="/login">Log in</a>
{% endif %}

Includes

html
{% include "partials/header.njk" %}
<main>{{ content }}</main>
{% include "partials/footer.njk" %}

Template Inheritance

Base template (base.njk):

html
<!DOCTYPE html>
<html>
<head>
  <title>{% block title %}Default{% endblock %}</title>
</head>
<body>
  {% block content %}{% endblock %}
</body>
</html>

Child template:

html
{% extends "base.njk" %}

{% block title %}Home{% endblock %}

{% block content %}
  <h1>Welcome</h1>
{% endblock %}

Multiple Pages

yaml
plugins:
  - @getkist/action-nunjucks

pipeline:
  build:
    stages:
      - name: pages
        steps:
          - action: TemplateRenderAction
            options:
              inputFile: src/templates/index.njk
              outputFile: dist/index.html
              dataFile: src/data/home.json
              
          - action: TemplateRenderAction
            options:
              inputFile: src/templates/about.njk
              outputFile: dist/about.html
              dataFile: src/data/about.json
              
          - action: TemplateRenderAction
            options:
              inputFile: src/templates/contact.njk
              outputFile: dist/contact.html
              dataFile: src/data/contact.json

With Environment Variables

yaml
- action: TemplateRenderAction
  options:
    inputFile: src/templates/index.njk
    outputFile: dist/index.html
    data:
      apiUrl: $&#123;&#123; env.API_URL &#125;&#125;
      version: $&#123;&#123; env.VERSION &#125;&#125;

Project Structure

Recommended structure:

src/
├── templates/
│   ├── base.njk
│   ├── index.njk
│   ├── about.njk
│   └── partials/
│       ├── header.njk
│       ├── footer.njk
│       └── nav.njk
└── data/
    ├── site.json
    ├── home.json
    └── about.json

Filters

Nunjucks includes built-in filters:

html
{{ title | upper }}
{{ description | truncate(100) }}
{{ date | date("YYYY-MM-DD") }}
{{ items | join(", ") }}
{{ content | safe }}

Released under the MIT License.