0

In my preprocessor I assign the following:

$variables['myVar'] = "value";

And in my twig file I can do the following and I get the value printed:

{{ myVar }}

If I however assign an array like this in my preprocessor:

$variables['myVar'] = ['name' => 'John','surname' => 'Doe'];

I am not able to get any output either through the outputting the variable, doing a dump or even trying to run it through a for loop:

{{ dump('myVar') }}
{% for items in myVar %}

I have cleared cache as well. Any ideas would be welcome :)

1
  • What do you get with {% for key,val in myVar %}{{ key }}: {{ val }}{% endfor %}? Did you "register" myVar with hook_theme or similar?
    – beltouche
    Commented Feb 22, 2022 at 18:39

1 Answer 1

0

in THEME.theme

function THEME_preprocess_page(&$variables)
{
  $variables['myVar'] = ['name' => 'John','surname' => 'Doe'];
}

in page.html.twig

name: {{ myVar.name }}
surname: {{ myVar.surname }}

or array iteration

{% for key,var in myVar %}
{{ key }}: {{ var }}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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