Set variable in parent scope in Twig

2019-03-15 21:04发布

In Smarty you can do

{$var = 'bla' scope=parent}

Is it possible in Twig?

Don't suggest to use blocks. I know. I need variable.

8条回答
The star\"
2楼-- · 2019-03-15 22:05

base.twig

<title>{{ title|default('example.com') }} - My cool site</title>

child.twig

{% set title = 'ChildTitle' %}
查看更多
姐就是有狂的资本
3楼-- · 2019-03-15 22:08

One other approach is to see twig configuration globally in your application. For example in Silex:

$app['twig']->addGlobal('someuser', $user);

You can then access that variable in all templates:

Hello, {{someuser.name}}
查看更多
登录 后发表回答