Arrow Functions in Twig

Map Filter & Arrow Functions in Twig

Examples of using a map filter in Twig.

Here are the code exam­ples Andrew reviews in the video to gen­er­ate a com­ma-delim­it­ed string from the source data at the bot­tom of this page.

Gen­er­ate a String Using a Loop

We start off with a basic imple­men­ta­tion using a for-loop to out­put a com­mand-delim­it­ed 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 %}

Gen­er­ate a String using map Fil­ter and Arrow Function

We drop the loop and replace it with the map fil­ter and arrow func­tion to cre­ate a com­ma-delim­it­ed string of item names and nick­names using map and an arrow function.

    {% set fullNames = ryansGoBag | map(
        (item, key) => "#{item.name} aka #{item.nickname}"
        )
        | join(', ') %}

    {% dd fullNames %}

Gen­er­ate a String using map and Craft Closure

Here we gen­er­ate the com­ma-delim­it­ed string of item names and nick­names using the map fil­ter 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 con­di­tion­al while build­ing the string of a com­ma-sep­a­rat­ed items. 

Gen­er­ate a String with a Con­di­tion­al using a Loop

    {% 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] %}

Gen­er­ate a String with a Con­di­tion­al Using map

In this exam­ple, we drop the loop and replace it with a map fil­ter to get a com­ma-delim­it­ed 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] %}

Gen­er­ate a String with a Con­di­tion­al using Craft Closure

In this exam­ple, we gen­er­ate a com­mand-delim­it­ed string of all names and only weapon names and nick­names using the map fil­ter and an arrow func­tion. We accom­plish this with an arrow vari­able 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] %}

Data for Examples

This is the data for all exam­ples 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,
} %}

Arrow Functions in Twig is made up of the following videos: