Map

Map

map filter

Return a new list with the given property from every object in the input list.

map: String property

Examples

Use the map filter to map blog posts to their linked title. The map filter could just as easily be used for any other property as well.

Map a list of blog posts to their linked titles

Copy
{% blog_posts posts = limit: 10 sort_by 'post_date' sort_direction:'desc' %}
{{ posts | map: 'linked_title' | join: '<br />' }}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{% datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" %}
{% datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder %}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

{% var one_rooms = houses | where: "rooms", "1" | sort: "price" %}
{% var cheapest = one_rooms | first %}
{% var mid_houses = one_rooms | slice: 1, 6 %}
{% var expensive_houses = one_rooms | slice: 7 %}
{% var num_expensive_houses = expensive_houses | size %}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %}
{% for list in grouped_by_rooms %}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %}
{% for list in mapped_by_rooms %}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{% endfor %}

{% map %}

Creates a new list of strings from the output when a block of liquid markup is executed for each item in a list or collection.

{% map [var, set, or assign] variable for item in collection reversed? [limit:num]? [offset:value]? %}

{% map
var, set, or assign
 
Optional. Specify either "var", "set" or "assign" to change which scope this map is stored on. "var" is the default behavior.
variable
for
variable_name
 
The name for the variable to assign each item in the collection to
in
collection
 
One or more values. May use the variable arguments syntax. The list of items to iterate
reversed
 
Causes the collection to be iterated in reverse order. Note that this takes effect after the offset and limit are applied
limit
offset
 
The number of items to skip at the beginning of the list. Alternatively, use the keyword "continue" to resume iteration of the list from the last time the same item and collection names were used in a map loop
%}

This method behaves exactly the same as the for method, 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 method as the capture method 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).

Examples

Map

Copy
{% map assign post_html for post in blogposts -%}
<h3>{{ post.linked_title }}</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 %}

Use the map filter to map blog posts to their linked title. The map filter could just as easily be used for any other property as well.

Map a list of blog posts to their linked titles

Copy
{% blog_posts posts = limit: 10 sort_by 'post_date' sort_direction:'desc' %}
{{ posts | map: 'linked_title' | join: '<br />' }}

To map a collection of entities into a human-readable list of titles linking to the entities:

Copy
{% for entity in collection.items -%}
{{ entity.linked_title }}{% unless forloop.first -%}{% if forloop.length == 2 %} and {% else %}, {% if forloop.last %}and {% endif %}{% endif %}{% endunless %}
{%- endfor %}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{% datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" %}
{% datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder %}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

{% var one_rooms = houses | where: "rooms", "1" | sort: "price" %}
{% var cheapest = one_rooms | first %}
{% var mid_houses = one_rooms | slice: 1, 6 %}
{% var expensive_houses = one_rooms | slice: 7 %}
{% var num_expensive_houses = expensive_houses | size %}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %}
{% for list in grouped_by_rooms %}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %}
{% for list in mapped_by_rooms %}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{% endfor %}