Symfony 2 和 TWIG 中的捆绑范围配置变量
我正在 Symfony 2 中编程,其中有两个包需要维护。他们的视图扩展了base.html.twig(应用程序范围的模板),如下所示:
{% extends '::base.html.twig' %}
...
现在base.html.twig看起来像:
...
<title>{{ page_title }}</title>
...
现在我的问题是如何以及在哪里定义变量page_titel,以便两个包具有不同的页面-标题。我知道,我可以在控制器的每个操作方法中的模板渲染中设置变量,但由于页面标题在一个包中是静态的,我正在寻找一种方法,我只需要定义变量一次。就像:
#app/config/config.yml
twig:
global:
foo: bar
但不是应用程序范围,而是捆绑范围! 有什么提示吗?
I'm programming in Symfony 2, where I have two bundles to maintain. Their views extend the base.html.twig (app-wide template) like:
{% extends '::base.html.twig' %}
...
Now the base.html.twig looks like:
...
<title>{{ page_title }}</title>
...
Now my question is how and where I can define the variable page_titel in order that the two bundles have different page-titles. I know, I could set the variable within the template-rendering in each action-method of the controller, but due the fact that the page-titel is static within one bundle, I'm searching an approach where I only have to define the variable once. Something like:
#app/config/config.yml
twig:
global:
foo: bar
but not application-wide, but bundle-wide!
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑阅读 TWIG 块。
在您的基本模板中使用:
然后在您的应用程序模板中使用:
标题将神奇地显示为标题内容。
我不能百分百确定我理解你的问题。但是您可以为每个包提供自己的base.html,该base.html 扩展主base.html 并设置标题。该捆绑包中的所有内容都会扩展捆绑包基础。所以标题只需要设置在一处即可。
Considering reading up on TWIG blocks.
http://symfony.com/doc/current/book/templating.html
In your base template use:
Then in your app templates use:
And the title will magically show up as the title content.
And I'm not 100% sure I understood your question. But you can give each bundle it's own base.html that extends the master base.html and sets the title. Everything in that bundle would then extend the bundle base. So the title would only need to be set in one spot.