Answer the question
In order to leave comments, you need to log in
How to use a loop in nunjucks for a template that is filled with a macro/object/array?
Hello everyone)
Googled a couple of solutions, but was not satisfied.
It's about nunjucks. I know for sure that you can have a template that can be run through a loop and set template properties in each iteration.
Task: There is a card template with variables inside for content. You need to run a loop that will take in an array or object with content for each individual card.
It seems like the question should be on the surface in Google, but for some reason I can’t find a solution.
And to do this: https://monosnap.com/file/FvjXLmQKEAytoToIuF70vYPo... I don’t feel like it at all)
Answer the question
In order to leave comments, you need to log in
something like this
{% set items = [
{img:'item-1', title:'111'},
{img:'item-2', title:'222'},
{img:'item-3', title:'333'}
] %}
{% import './macros/_item-block.html' as itemBlock %}
{% for item in items %}
<div class="section-items__item">
{{ itemBlock.item(item.img, item.title) }}
</div>
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question