I am a nunjucks newbie - trying it out along with eleventy.
Short version: can I use variables and filters within tags in nunjucks? For example:
{% set myVar = {{ title | lower }} %}
(assume the {{ title }} variable is set correctly)
Longer version / my specific use case:
I am trying to create a layout file called section.njk and use it for several pages (basically the section frontpages of each section of my site - similar idea to a section.html layout file in Hugo) I have a data file for each section, containing the menu for that section.
Given the following files:
This does not work:
{% for item in {{ title | lower }} %}
<a href="{{ item.url | url }}"><div>{{ item.title }}</div></a>
{% endfor %}
This works:
{% for item in guides %}
<a href="{{ item.url | url }}"><div>{{ item.title }}</div></a>
{% endfor %}
So I'm wondering if the problem is using variables within tags in Nunjucks, and if so, if there's a way round it?
Entire project source is here: https://github.com/StarfallProjects/tech-writer-toolkit/tree/11ty (the 11ty stuff is in src)
Thanks for any help.
Yes, you can.
{% set myVar = title | lower %}
{% set myVar = title | lower + var2 | upper %}
{% set myVar = (title | lower + var2) | trim %}
...
{% for item in items | sort(false, true) %}
<a href="{{ item.url | url }}"><div>{{ item.title | lower}}</div></a>
{% endfor %}
The lower, upper and trim filters are applyed to number/string vars. They can't be applyed for some array (or object).
On the another side, the sort-filter expects an array as input.
The filter "type" is obvious by its description in docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With