Symfony 2 和 TWIG 中的捆绑范围配置变量

发布于 2025-01-05 08:44:31 字数 483 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2025-01-12 08:44:31

考虑阅读 TWIG 块。

在您的基本模板中使用:

<title>{% block title %}{% endblock %}</title>

然后在您的应用程序模板中使用:

{% block title %}Bundle 1 Title{% endblock %}

标题将神奇地显示为标题内容。

我不能百分百确定我理解你的问题。但是您可以为每个包提供自己的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:

<title>{% block title %}{% endblock %}</title>

Then in your app templates use:

{% block title %}Bundle 1 Title{% endblock %}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文