{% map %}

The {% map %} tag repeatedly executes a block of code for every item in a list or collection, and saves the result as a list of strings.

Syntax

{% map [var|set|assign] new_variable_name for itemname in collection [offset: number] [limit: number] [reversed] %}...{% endfor %}

This tag behaves exactly the same as the for tag, except that instead of outputting the results directly to the template, it saves them in a list instead. It may be helpful to think of the map tag as the capture tag for lists. You can even assign varaibles by reference (eg: {% map &newcollection for item in oldcollection %}).

Note that you can also use the forloop properties and tags inside a map block (see the for tag documentation for details).

The map tag creates a new child scope.

Examples

Map

Copy
{% map assign post_html for post in blogposts -%}

{{ post.linked_title }}

{% if post.image.is_valid %}

{% img post.image link:post.default_page_url preset:"thumb" title:post.title %}

{% endif %}
{{ post.summary_html }}
{%- endmap %}
{{post_html[0]}}
{% let smallcontent = post_html | slice: 1 %} {% include "_small_content" contents:smallcontent %}