htmlnode.jstemplate-enginenunjucksposthtml

How to automatically repeat HTML content in nunjucks?


This PostHTML plugin "PostHTML Each" can repeat HTML code in a simple way. Like this

<!-- BEFORE -->
<div class="block" each="3"></div>

<!-- AFTER -->
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>

How to repeat in a similar way in Nunjucks?


Solution

  • I don't think there's a nice one-liner as in your example for PostHTML but as mentioned in the comment you can use for tag. It needs a data source to iterate over, so use it with range function which will provide such.

    {% for i in range(0, 3) -%}
      <div class="block"></div>
    {%- endfor %}
    

    Docs: