0

I have a php variable in my controller and i want to use it in twig. So i do this :

Controller :

$firstLetter = '2017'; //dynamic variable but for the test it's a constant 

$build ['list'] = array(
      '#theme' => 'liste-adh2',
      '#content'=>$result,
      '#filters'=>$ListApport,
      '#filters2' =>$Listline,
      '#test' => $firstLetter,  //sending to my template
   );  

return $build;

Template :

function adh_apport_theme($existing, $type, $theme, $path)
{

    return [
        'liste-adh2' => [
            'variables' => ['content' => NULL, 'filters' => NULL,
                            'filters2' => NULL,], ['test' => NULL,],
                        ],
            ];
}

Twig :

{{ test.firstLetter }}

i got nothing in twig and i don't know why.

Thanks for helping.

1

I’m not sure why you’re expecting test to be an object, you declared it as a string.

Assuming the value of “2017” from your example is what you’re trying to get the variable is just:

{{ test }}
  • Omg i'm so bad, you save my day, really thank you for helping. Have a nice day :D – psilonnix Apr 25 '18 at 7:18

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.