carlitoplatanito / gulp-nunjucks-render

[Gulp](https://github.com/wearefractal/gulp) plugin to render [Nunjucks](http://mozilla.github.io/nunjucks/) templates
149 stars 33 forks source link

{% with %} error #48

Closed floatrx closed 6 years ago

floatrx commented 8 years ago

When i trying to use {% with %} i received an error message on build...

{% with foo = 'bar' %}
  {% include 'something.else' %}
{% endwith %}
kamlekar commented 8 years ago

Can't find anything related to with in nunjucks templating docs. Are you sure this syntax is present?

TheDancingCode commented 6 years ago

The {% with %} tag doesn't exist. To pass a variable to an include, you can either do this:

<!-- index.html -->
{% set name = "Charles Dickens" %}
{% set year = 1812 %}
{% include "author.html" %}
<!-- author.html -->
<h1>{{ name }}</h1>
<p>{{ name }} was born in {{ year }}.</p>

Or use a macro, like this:

<!-- index.html -->
{% from "author.html" import author %}
{{ author("Charles Dickens", 1812) }}
<!-- author.html -->
{% macro author(name, year) %}
  <h1>{{ name }}</h1>
  <p>{{ name }} was born in {{ year }}.</p>
{% endmacro %}

Both will render:

<!-- index.html -->
<h1>Charles Dickens</h1>
<p>Charles Dickens was born in 1812.</p>