2

小枝でブロックを追加する方法についていくつかの質問があります。答えは常に継承を使用し、使用してからparent()を呼び出すことです。どういうわけか、これが私の特定のケースでどのように機能するのかわかりません:

base.html.twig

{% block content %}{% endblock %}
{% block appendable %}
{% endblock %}
{% block another_appendable %}
{% endblock %}

site.html.twig

{% extends base.html.twig %}
{% block content %}
{# Here use/include/embed, i dont know #}
{% use sub1.html.twig %}
{% use sub2.html.twig %}
{% endblock content %}

sub1.html.twig

Some content that should be directly rendered
{% block appendable %}
some stuff that should be added to appendable
{% endblock %}
{% block another_appendable %}
This content should be added to "another appendable"
{% endblock %}

sub2.html.twig

{% block appendable %}
additional stuff that should be appended
{% endblock %}

sub1 と sub2 の両方のコンテンツが追加可能内にレンダリングされることを望みます。どうすればそれを達成できますか?

4

4 に答える 4

2

includeテンプレートを含めるには、キーワードではなくキーワードを使用する必要がありますuse

{% block appendable %}

    {# Assuming your sub1 template is in AcmeDemoBundle/Resources/views/MySub/sub1.html.twig #}
    {% include "AcmeDemoBundle:MySub:sub1.html.twig" %}

{% endblock appendable %}

AcmeDemoBundle:MySub:sub1.html.twigは次のようになります。

<b>Put directly your code there, no need to use the blocks.</b>

継承の使用

必要に応じて、{{ parent() }}キーワードを使用して継承を使用できます。たとえば、sub1.html.twigデフォルトで含めたいがsub2.html.twig、子テンプレートに追加したい場合は、次のことができます。

base.html.twig

{% block content %}

    {% include "AcmeDemoBundle:MySub:sub1.html.twig" %}

{% endblock %}

site.html.twig

{% extends base.html.twig %}

{% block content %}

    {# render what happens in the parent content block #}
    {{ parent() }}

    {# and append sub2.html.twig as well #}
    {% include "AcmeDemoBundle:MySub:sub2.html.twig" %}

{% endblock content %}
于 2013-01-28T14:48:03.677 に答える