Examples of using a map filter in Twig.
Here are the code examples Andrew reviews in the video to generate a comma-delimited string from the source data at the bottom of this page.
We start off with a basic implementation using a for-loop to output a command-delimited string of item names. and nicknames.
{% set fullNames = "" %}
{% for item in ryansGoBag %}
{% set fullNames = fullNames ~ "#{item.name} aka #{item.nickname}" %}
{% if not loop.last %}
{% set fullNames = fullNames ~ ", " %}
{% endif %}
{% endfor %}
{% dd fullNames %}
map Filter and Arrow FunctionWe drop the loop and replace it with the map filter and arrow function to create a comma-delimited string of item names and nicknames using map and an arrow function.
{% set fullNames = ryansGoBag | map(
(item, key) => "#{item.name} aka #{item.nickname}"
)
| join(', ') %}
{% dd fullNames %}
map and Craft ClosureHere we generate the comma-delimited string of item names and nicknames using the map filter with Craft Closure.
{% set fullNamesArrow = (item, key) => "#{item.name} aka #{item.nickname}" %}
{% set fullNames = ryansGoBag | map(fullNamesArrow) | join(', ') %}
{% dd fullNames %}
Andrew then moves on to using a conditional while building the string of a comma-separated items.
{% set fullNames = "" %}
{% set fullNamesOfWeapons = "" %}
{% for item in ryansGoBag %}
{% set fullNames = fullNames ~ "#{item.name} aka #{item.nickname}" %}
{% if not loop.last %}
{% set fullNames = fullNames ~ ", " %}
{% endif %}
{% if item.type == "weapon" %}
{% set fullNaxmesOfWeapons = fullNamesOfWeapons ~ "#{item.name} aka #{item.nickname}" %}
{% if not loop.last %}
{% set fullNamesOfWeapons = fullNamesOfWeapons ~ ", " %}
{% endif %}
{% endif %}
{% endfor %}
{% dd [fullNames, fullNamesOfWeapons] %}
mapIn this example, we drop the loop and replace it with a map filter to get a comma-delimited string of all names and only weapon names and nicknames.
{% set weapons = ryansGoBag | filter(
(item) => item.type == "weapon"
) %}
{% set fullNamesOfWeapons = weapons | map(
(item, key) => "#{item.name} aka #{item.nickname}"
)
| join(', ') %}
{% set fullNames = ryansGoBag | map(
(item, key) => "#{item.name} aka #{item.nickname}"
)
| join(', ') %}
{% dd [fullNames, fullNamesOfWeapons] %}
In this example, we generate a command-delimited string of all names and only weapon names and nicknames using the map filter and an arrow function. We accomplish this with an arrow variable with Craft Closure.
{% set weaponsArrow = (item) => item.type == "weapon" %}
{% set fullNamesArrow = (item, key) => "#{item.name} aka #{item.nickname}" %}
{% set weapons = ryansGoBag | filter(weaponsArrow) %}
{% set fullNamesOfWeapons = weapons | map(fullNamesArrow) | join(', ') %}
{% set fullNames = ryansGoBag | map(fullNamesArrow) | join(', ') %}
{% dd [fullNames, fullNamesOfWeapons] %}
This is the data for all examples used in this course:
{% set ryansGoBag = [
{
type: "money",
name: "Dollars",
nickname: "Greenbacks",
value: 3000,
},
{
type: "money",
name: "Euros",
nickname: "Yoyos",
value: 1000,
},
{
type: "money",
name: "Rubles",
nickname: "Barnie & Bettie",
value: 181500.00,
},
{
type: "weapon",
name: "Gun",
nickname: "Piece",
value: 320.00,
},
{
type: "weapon",
name: "Trebuchets",
nickname: "Kitty Launcher",
value: 1500.00,
},
{
type: "weapon",
name: "Caltrops",
nickname: "Crow's Foot",
value: 52.00,
},
{
type: "food",
name: "Brocolli Crudités",
nickname: "Lawn Clippings",
value: 0.0,
},
{
type: "food",
name: "Golden Tofu",
nickname: "Spoiled Jello",
value: 0.0,
},
{
type: "food",
name: "Natto Beans",
nickname: "Baked Fart",
value: 0.0,
},
] %}
{% set conversionRates = {
"Dollars" : 1.0,
"Euros" : 1.02,
"Rubles" : 0.017,
} %}

Andrew is a regular contributor to CraftQuest through the CraftQuest on Call livestream. He's the developer behind the most popular Craft CMS plugins, like SEOmatic and Retour. Andrew has decades of experience in software development for both web and client applications.