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 Answer 1

0

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 }}
1
  • Omg i'm so bad, you save my day, really thank you for helping. Have a nice day :D
    – user80337
    Commented Apr 25, 2018 at 7:18

Your Answer

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