Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering and splicing an array in Twig

I have an array of user records (0 indexed, from a database query), each of which contains an array of fields (indexed by field name). For example:

Array
(
    [0] => Array
        (
            [name] => Fred
            [age] => 42
        )

    [1] => Array
        (
            [name] => Alice
            [age] => 42
        )

    [2] => Array
        (
            [name] => Eve
            [age] => 24
        )

)

In my Twig template, I want to get all the users where the age field is 42 and then return the name field of those users as an array. I can then pass that array to join(<br>) to print one name per line.

For example, if the age was 42 I would expect Twig to output:

Fred<br>
Alice

Is this possible to do in Twig out of the box, or would I need to write a custom filter? I'm not sure how to describe what I want in a couple of words so it may be that someone else has written a filter but I can't find it by searching.

like image 849
pwaring Avatar asked Jan 17 '17 16:01

pwaring


People also ask

How do you split in Twig?

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default). Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.

What are filters in Twig?

Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

How do you access an array in Twig?

Accessing array elements Twig as a parameter can receive array. To access a specific element of array you can use regular php array access bracket notation {{ array[key] }} .


2 Answers

Final solution was a mix of what has been posted so far, with a couple of changes. The pseudocode is:

for each user
  create empty array of matches
  if current user matches criteria then
    add user to matches array
join array of matches

Twig code:

{% set matched_users = [] %}
  {% for user in users %}
    {% if user.age == 42 %}
      {% set matched_users = matched_users|merge([user.name|e]) %}
    {% endif %}
  {% endfor %}
  {{ matched_users|join('<br>')|raw }}

merge will only accept an array or Traversable as the argument so you have to convert the user.name string to a single-element array by enclosing it in []. You also need to escape user.name and use raw, otherwise <br> will be converted into &lt;br&gt; (in this case I want the user's name escaped because it comes from an untrusted source, whereas the line break is a string I've specified).

like image 85
pwaring Avatar answered Oct 09 '22 20:10

pwaring


In twig you can merge the for ( .... in ....) with the if condition like :

{% for user in users if user.age == 42 %}
    {{ user.name }}{{ !loop.last ? '<br>' }}
{% endfor %}

Edit: This syntax is deprecated, and we are advised to use |filter as a replacement for the for...if syntax.

Twig Filter: filter (The name of the filter is filter)

Twig Deprecated Features

like image 35
xaper Avatar answered Oct 09 '22 21:10

xaper