【发布时间】:2012-08-23 08:20:08
【问题描述】:
我使用 symfony2 和 TWIG,我想使用一些全局变量来指向页面中平板电脑的索引:
app/config/parameters.ini
tab_branch="1"
app/config/config.yml
twig:
globals:
tab_branch: %tab_branch%
src/ACME/BranchBundle/Controller/defaultController.php
/**
* @Template()
*/
public function showAction($id) {
...
return array(
'tab' => 'tab_branch',
);
}
src/ACME/BranchBundle/Resources/views/default/show.html.twig
<input type="hidden" id="tablndex" value="{{ {{ tab }} }}" /> //not working
我真正想做的是将{{ tab }}解析为tab_branch,然后将{{ tab_branch }}解析为1(就像PHP中的$$foo一样),我该怎么办?
【问题讨论】:
-
对不起,但这只是代码味道不好。维持这将是一场噩梦。您能否详细说明您要解决的问题到底是什么?
-
感谢您的建议。我真正想做的只是标记当前选项卡。现在我使用querystring在页面之间传递tabindex,即abc.com/branch?idx=1,但我真的想丢弃querystring,你能建议其他解决方法吗?
-
根据网站的复杂程度,您可以检查模板中的当前路线 (
{{ app.request.attributes.get('_route') == 'FooBar' ? 'foo' : 'bar' }}) 或选择更复杂的内容,例如 writing a custom twig extension 或使用 KnpMenuBundle。跨度>