1

I tried to do something that I thought was fairly simple, but it's giving an error:

Notice: Array to string conversion in __TwigTemplate_dd3a187031be000574aa6b2149e8b2daacd4f1d086638752b5928811c46b8adf->doDisplay() (line 132 of vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code).

What caused it:

{% set support_url = url('<front>') ~ 'support' %}

So I tried something:

{{ url('<front>') }}<br />
{% set support_url = url('<front>') %}
{{ support_url }}<br />
{% set support_url = url('<front>') ~ 'support' %}
{{ support_url }}<br />

The result of the above is:

http://www.example.com/

http://www.example.com/

Arraysupport

What is going on?

  • 1
    url() returns a render array, which Twig knows how to render. At the moment you're concatenating a string and an array, hence the output – Clive Aug 11 '17 at 14:22
  • I see. So I suppose there is no way to do what I was trying. I would have to pass the front page URL as a variable in preprocess. – Rob Aug 11 '17 at 14:25
  • Ah, you helped me find how. It's easy: {% set support_url = url('<front>')|render ~ 'support' %} – Rob Aug 11 '17 at 14:27
  • 1
    That doesn't really make sense however. If you want to builda different url, you should provide a different route to url() ? If you go there, why bother with url() at all, you could just as well just hardcode the URL in the first place? – Berdir Aug 11 '17 at 14:28
  • The URL doesn't have a route, and I'm printing the link inside some translated text. – Rob Aug 11 '17 at 14:29
3

The url function returns a render array, so you can't concatenate it with a string. You must first convert it to a string.

try render_var():

{% set support_url = render_var(url('<front>')) ~ 'support' %}

<h1>{{ support_url }}</h1>

This will convert the render array created by the url function to a string.

| improve this answer | |
0

I do not know if my contribution is useful, but I had a problem with a multilanguage site and I solved it in this way.

The drawback was, that the language is not shown /ES, / EN, /pt-br, etc. It always showed the url of the base language, that is, EN.

{% set support_url = path('<front>') ~ '/node/102' %}

{% set base_url = render_var(url('<front>')) ~ support_url %} 

<a href="{{ base_url }}">
    <h2 class="block-title">{{ 'Change'|t}}    </h2>
</a>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.