Nunjucks Documentation

repository·master·Indexed 27 days ago

https://github.com/mozilla/nunjucks

A full-featured templating engine for JavaScript inspired by Jinja2, featuring inheritance and asynchronous control. Documentation covers the Environment and Template classes, loaders (FileSystemLoader, NodeResolveLoader, WebLoader), and the precompile CLI for converting templates into JavaScript code. Includes guides on Express integration and Jinja compatibility.

Tokens
18.6K
Snippets
73
Records
134
Agent score
93%

What's inside nunjucks

  1. Use Nunjucks in the browser

    master

    You can include Nunjucks in the browser using a script tag or as an AMD module.

    Choosing a build

    • nunjucks.js: The full library (approx. 20K min/gzipped). Includes the full compiler, allowing you to dynamically load and compile templates on the fly. Recommended for development or if file size is not a priority.
    • nunjucks-slim.js: The slim version (approx. 8K min/gzipped). Does not include the compiler and only works with precompiled templates. Recommended for production.

    Loading methods

    Script tag:

    <script src="nunjucks.js"></script>

    AMD module:

    define(['nunjucks'], function(nunjucks) {
    });
  2. Use filters to transform variables

    master

    Filters are functions that operate on variables, invoked using the pipe operator (|). Filters can accept arguments and can be chained together to perform multiple transformations in sequence.

    {{ foo | title }}
    {{ foo | join(",") }}
    {{ foo | replace("foo", "bar") | capitalize }}
  3. Use variables in Nunjucks templates

    master

    Variables are retrieved from the template context. You can display a variable using double curly braces {{ ... }}. You can access object properties using either dot notation or bracket notation, similar to JavaScript.

    Note on null/undefined: If a variable's value is undefined or null, nothing will be rendered. This also applies to accessing properties of an undefined object (e.g., if foo is undefined, {{ foo.bar }} will not render).

    {{ username }}
    {{ foo.bar }}
    {{ foo["bar"] }}
  4. Use keyword arguments in functions, filters, and macros

    master

    Nunjucks supports keyword arguments (similar to Python) for functions, filters, and macros. When you use keyword arguments in a template, Nunjucks converts them into a single object passed as the last argument to the underlying JavaScript function.

    Example of calling a function with keyword arguments:

    {{ foo(1, 2, bar=3, baz=4) }}

    Equivalent JavaScript call:

    foo(1, 2, { bar: 3, baz: 4 });

    You can also define macros with default values for keyword arguments. Nunjucks will automatically match the provided keyword arguments to the defined values.

  5. Use expressions and math in templates

    master

    Nunjucks supports JavaScript-like literals (Strings, Numbers, Arrays, Dicts, Booleans) and mathematical operators:

    • Addition: +
    • Subtraction: -
    • Division: /
    • Integer division: //
    • Remainder: %
    • Multiplication: *
    • Power: **

    Note: It is recommended to keep complex logic in your JavaScript code rather than in the template.

  6. Use inline if expressions

    master

    Nunjucks supports an inline if expression similar to JavaScript's ternary operator. The else part is optional.

    Syntax: {{ value_if_true if condition else value_if_false }}

    {{ "true" if foo else "false" }}
    {{ baz(foo if foo else "default") }}
    {{ "true" if foo }}