26

This is probably relatively easy to do, but I'm new to twig and I'm frustrated.

I'm adapting code from this answer: https://stackoverflow.com/a/24058447

the array is made in PHP through this format:

$link[] = array(
       'link' => 'http://example.org',
       'title' => 'Link Title',
       'display' => 'Text to display',
);

Then through twig, I add html to it, before imploding:

    <ul class="conr">
        <li><span>{{ lang_common['Topic searches'] }} 
        {% set info = [] %}
        {% for status in status_info %}
            {% set info = info|merge(['<a href="{{ status[\'link\'] }}" title="{{ status[\'title\'] }}">{{ status[\'display\'] }}</a>']) %}
        {% endfor %}

        {{ [info]|join(' | ') }}
    </ul>

But I'm getting:

Errno [8] Array to string conversion in F:\localhost\www\twig\include\lib\Twig\Extension\Core.php on line 832

It's fixed when I remove this line, but does not display:

{{ [info]|join(' | ') }}

Any ideas how I can implode this properly?

** update **

Using Twig's dump function it returns nothing. It seems it's not even loading it into the array in the first place. How can I load info into a new array.

Community
  • 1
  • 1
Chris98
  • 567
  • 1
  • 4
  • 16
  • if you're reading this in 2023, [`join`](https://twig.symfony.com/doc/1.x/filters/join.html) supports two parameters: `glue`, `and` ("_`and`: The separator for the last pair of input items [...] The `and` argument was added in Twig 1.37._") – Sandra Jan 18 '23 at 11:35

4 Answers4

57

info is an array, so you should simple write

{{ info|join(', ') }}

to display your info array.

[info] is a array with one value : the array info.

JL M
  • 1,378
  • 1
  • 10
  • 14
  • NB : Only working with values in case of associative array – Delphine Nov 17 '17 at 15:39
  • 4
    @Delphine If you want to join the keys of an associative array you could pass the array through the `keys` filter first, i.e. `{{ info | keys | join(', ') }}` – Dan Abrey Dec 14 '17 at 11:31
15

You shouldn't really be building complex data structures inside of Twig templates. You can achieve the desired result in a more idiomatic and readable way like this:

{% for status in status_info %}
    <a href="{{ status.link }}" title="{{ status.title }}">{{ status.display }}</a>
    {% if not loop.last %}|{% endif %}
{% endfor %}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • The problem is though imploding it. I need each element separated by an ' | ' but when I manually do this there is one extra "|" on the right side of the page. That's the only way I can think of to get only the needed amount. – Chris98 Sep 17 '15 at 15:40
  • Uhm... that's what ***`if not loop.last`*** is supposed to prevent...!? – deceze Sep 17 '15 at 15:41
  • Ok. Thanks. I'd never heard of that. I'm pretty new to Twig. – Chris98 Sep 17 '15 at 15:41
13

You can user json_encode for serialize array as strig, then show pretty - build in twig

    {{ array|json_encode(constant('JSON_PRETTY_PRINT')) }} 
    
Developer
  • 2,731
  • 2
  • 41
  • 71
  • 3
    Please explain that further - what does your code do? Keep in mind that others should be able to learn from that code – Nico Haase Jun 28 '19 at 14:48
  • This is the best answer, because it'll convert the whole structure into a string, whereas `join` will fail if the variable is a nested array (e.g. try `['a', ['b'] ]`). Useful for debugging. – Fabien Snauwaert Apr 02 '20 at 08:52
2

if need associative array:

{{info|json_encode(constant('JSON_PRETTY_PRINT'))|raw}}
Georg
  • 35
  • 2