{% 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 assign post_html for post in blogposts -%}
    <h3>{{ post.linked_title }}&lgt;/h3>
    {% if post.image.is_valid %}
        <p>{% img post.image link:post.default_page_url preset:"thumb" title:post.title %}</p>
    {% endif %}
    <div class="summary">{{ post.summary_html }}</div>
{%- endmap %}
<div class="mainpost">{{post_html[0]}}</div>
{% let smallcontent = post_html | slice: 1 %}
{% include "_small_content" contents:smallcontent %}

Developer Overview

Liquid Markup