Score:0

Cannot get any output when passing array through preprocessor

cn flag

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

beltouche avatar
cn flag
What do you get with {% for key,val in myVar %}{{ key }}: {{ val }}{% endfor %}? Did you "register" myVar with hook_theme or similar?
Score:0
cn flag

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 %}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.