Reputation: 5418
I am using Nunjucks as the template engine for my project, where I have the following scenario.
The base file can use the layout and page level variable and generate final HTML.
Base file: base.html
<html class="{{htmlClass}}"> .. </html>
Layout file: layout.html
...
{% extends "html/_layouts/base.html" %}
{% set htmlClass = "class-1" %}
...
Page file: page.html
...
{% extends "html/_layouts/layout.html" %}
{% set htmlClass = "class-2" %}
...
The actual generated HTML file has:
<html class="class-1"> .. </html>
Expected generated HTML file should have (it should append the page level variable value) :
<html class="class-2"> .. </html>
Please consider that I do not want to use two separate variables.
Previously I was using the PUG and I was using block htmlClass
and to override value append htmlClass
with a variable.
I have tried to find a similar way for Nunjucks, unfortunately, I can't find append
.
Thanks in advance 😇
Upvotes: 3
Views: 949
Reputation: 5418
Finally, I found the solution below.
{% set htmlClass = htmlClass | default('class-2') %}
Now I am able overriding variable value from end file using Nunjucks.
Upvotes: 4
Reputation: 64
try
{% set htmlClass = "class-2" %}
{% extends "html/_layouts/layout.html" %}
instead of:
{% extends "html/_layouts/layout.html" %}
{% set htmlClass = "class-2" %}
set before extends
Upvotes: 0