Nunjucks: Variables and macros

Created on 4 Aug 2017  路  2Comments  路  Source: mozilla/nunjucks

I have a page with some variables defined in it, and I import a macro:

{% extends "layout.nunjucks" %}
{% set activePage = "iis" %}
{% set activeSubPage = "organos" %}

{% block content %}
<!--sub-nav-->
{% import 'macros/subnavigation.nunjucks' as subnav %}
{{subnav.active(activeSubPage)}}
<!--/sub-nav-->
{% endblock %}

Inside the macro, I want to check the content of the activePage variable:
````
{% macro active(activeSubPage='quienes-somos') %}

{% endmacro %}
```
As activePage == 'iis', it should render 'subnav-1.nunjucks', but instead it renders

 

The question is: can I reference a variable in a macro, which is defined outside of it?

Thanks.

Most helpful comment

You need to import macro with current context, like so:

{% import 'macros/subnavigation.nunjucks' as subnav with context %}

All 2 comments

You need to import macro with current context, like so:

{% import 'macros/subnavigation.nunjucks' as subnav with context %}

Great, didn't know about that.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sandiprb picture sandiprb  路  5Comments

wslx520 picture wslx520  路  4Comments

kamlekar picture kamlekar  路  5Comments

mintyPT picture mintyPT  路  4Comments

ziir picture ziir  路  3Comments