Twig Array to convert strings

This is probably relatively easy to do, but I'm new to the branch and I'm upset.

I am adapting the code from this answer: stack overflow

an array is created in PHP in this format:

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

Then, through twig, I add html to it before it is taken:

    <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 get:

Errno [8] Converting an array to a string in F: \ localhost \ www \ twig \ include \ lib \ Twig \ Extension \ Core.php on line 832

Fixed when I delete this line but not showing:

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

Any ideas on how I can blow this properly?

** update **

Using the Twig dump function, it returns nothing. It doesn't seem to even load it into an array in the first place. How to load information into a new array.

+14
3

Twig. :

{% for status in status_info %}
    <a href="{{ status.link }}" title="{{ status.title }}">{{ status.display }}</a>
    {% if not loop.last %}|{% endif %}
{% endfor %}
+9

info - ,

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

.

[info] - : .

+31

You can use json_encode to serialize the array as a strig, and then show pretty - inline the branch

    {{array | json_encode (constant ('JSON_PRETTY_PRINT'))}} 
    
+1
source

Source: https://habr.com/ru/post/1607844/


All Articles