Set variable in parent scope in Twig
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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. 回答1: base.twig <title>{{ title|default('example.com') }} - My cool site</title> child.twig {% set title = 'ChildTitle' %} 回答2: If you don't want to use the default() filter (i.e., when you want to use the variable multiple times throughout your parent and child templates), you can actually define a block that contains your entire page in the parent template, and then nest your other blocks inside of that: {# base