Twig 包含模板扩展父块一次 [英] Twig included template extending parent's block once

查看:24
本文介绍了Twig 包含模板扩展父块一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到这一点?我有一个输出一篇博客文章的模板.

Is there a way to do this? I have a template which outputs one blog article.

现在,在 index 页面上,我通过将该模板包含在 for 循环中来显示 10 篇文章,而在 show 页面上我只显示一篇文章.

Now, on index page I show 10 articles by including that template in for loop and on show page I only show one.

索引:

{% block stylesheets %}
    {# some stylesheets here #}
{% endblock %}

{% for article in articles %}
        {% include VendorBundle:article.html.twig with { 'article': article } %}
{% endfor %}

显示:

{% block stylesheets %}
      {# some stylesheets here #}
{% endblock %}

{% include VendorBundle:article.html.twig with { 'article': article } %}

现在有没有办法让 article.html.twig 向自动包含它的模板的 {% block stylesheets %} 添加一些东西?如果可能,如何在使用 for 循环时防止它添加 10 次?

Now is there a way to make article.html.twig add something to {% block stylesheets %} of templates that included it automatically? If it is possible, how do I prevent it from adding that 10 times when using for loop?

我正在尝试让我的片段"模板(用于包含的模板)定义它们使用的样式表,并将它们注入"到页面中.

I'm trying to make my "fragment" templates (templates used for inclusion) define stylesheets that they use and make them "inject" those into page.

推荐答案

您是否尝试使用 使用?不幸的是,我不完全确定我的问题是否正确,但这里没有提到 {% use %}.

据我所知,您已经获得了 article.html.twig 并将其包含在例如index.html.twig.现在您想将 article.html.twig 中的内容添加到 index.html.twig 中吗?即到 {% stylesheets %} 块.

As I understand the question you've got your article.html.twig and include it in e.g. index.html.twig. Now you want to add something from article.html.twig into index.html.twig? Namely to the {% stylesheets %} block.

如果我知道如何使用 {% use %} 你也许可以这样试试.

If I got how to use {% use %} you could maybe try it like this.

article.html.twig

{% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('bundles/mybundle/css/article.css') }}" type="text/css" />
{% endblock %}
{% block article %}
    {# whatever you do here #}
{% endblock %}

index.html.twig

{% use "VendorBundle:article.html.twig" with stylesheets as article_styles %}
{% block stylesheets %}
    {{ block('article_styles') }}
    {# other styles here #}
{% endblock %}
{% for article in articles %}
        {% include VendorBundle:article.html.twig with { 'article': article } %}
{% endfor %}

我没有机会测试它,但文档说明了一些非常有趣的事情,似乎这可能是实现它的方式.

I have not the chance to test it but the docu states a few very interesting things and it seems like this could be the way to do it.

水平重用是一项高级 Twig 功能,在常规模板中几乎不需要.主要用于需要在不使用继承的情况下使模板块可重用的项目.

Horizontal reuse is an advanced Twig feature that is hardly ever needed in regular templates. It is mainly used by projects that need to make template blocks reusable without using inheritance.

我对 stackoverflow 比较陌生.所以,如果我的回答完全没用,你能不能在投票前发表评论然后我删除它?但是,如果它确实有帮助并且我的示例中只有一些错误,也请通知我,我会修复它.

I am rather new to stackoverflow. So please if my answer is completely useless could you just post a comment before down voting and I delete it? However if it does help and there are just some errors in my example, also inform me and I'll fix it.

这篇关于Twig 包含模板扩展父块一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆