简体   繁体   中英

Render object as context in twig template

How to render an object instead of array, like we usually do?

echo $twig->render('index.html', array('name' => 'Fabien'));

The render() function does not accept an object.

Is there any way to render the object directly?.

And I do not mean an "objectToArray" solution.

The second parameter of the method render take an array for transport data to the view, so you simply put your object as value of the array with a specified key. Something like this:

$object = new People()
$object->setName('Fabien');
echo $twig->render('index.html', array('obj' => $object));

And use in the template as

{{ obj.name }}

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM