然后在Twig模板上包含block

时间:2013-05-08 16:42:50

标签: symfony twig template-inheritance

之前的任何问题似乎都没有任何信息,大约一天半的随机Google搜索都没有显示出来。

我想要做的是,我有一个base.html.twig模板,然后是一个包含多个ui元素的文件夹(幻灯片,导航菜单等)。我应该能够做到这一点:

page.html.twig包含

{% extends 'OnMyLemonCommonBundle::base.html.twig' %}

{% block content %}

  {% include 'OnMyLemonCommonBundle::features.html.twig' %}

  {% block features %}

    <h2>Featured Articles</h2>

    <li><a href="#">Article 1</a></li>
    <li><a href="#">Article 2</a></li>

  {% endblock %}

{% endblock %}

features.html.twig包含

<div class="row">
  <div class="large-12 columns">
    <div class="row">

      <div class="large-4 small-6 columns">
        {% block features %}{% endblock %}
      </div>

      <div class="large-4 small-6 columns">
        {% block blogs %}{% endblock %}
      </div>

      <div class="large-4 small-12 columns">
        {% block pictures %}{% endblock %}
      </div>

    </div>
  </div>
</div>

问题是,这将呈现如下:

// Content from top of base.html.twig

<div class="row">
  <div class="large-12 columns">
    <div class="row">

      <div class="large-4 small-6 columns">
      </div>

      <div class="large-4 small-6 columns">
      </div>

      <div class="large-4 small-12 columns">
      </div>

    </div>
  </div>
</div>

<h2>Featured Articles</h2>

<li><a href="#">Article 1</a></li>
<li><a href="#">Article 2</a></li>

// Content from bottom of base.html.twig

问题是如何将此输出设为以下内容:

// Content from top of base.html.twig

<div class="row">
  <div class="large-12 columns">
    <div class="row">

      <div class="large-4 small-6 columns">

        <h2>Featured Articles</h2>

        <li><a href="#">Article 1</a></li>
        <li><a href="#">Article 2</a></li>

      </div>

      <div class="large-4 small-6 columns">
      </div>

      <div class="large-4 small-12 columns">
      </div>

    </div>
  </div>
</div>

// Content from bottom of base.html.twig

2 个答案:

答案 0 :(得分:1)

您可以通过在twig文件中添加额外的继承层来实现此目的,例如:

features-base.html.twig包含:

{% extends 'OnMyLemonCommonBundle::base.html.twig' %}

{% block content %}

  {% include 'OnMyLemonCommYonBundle::features.html.twig' %}

{% endblock %}

page.html.twig包含:

{% extends 'OnMyLemonCommonBundle::features-base.html.twig' %}

{% block features %}

  <h2>Featured Articles</h2>

  <li><a href="#">Article 1</a></li>
  <li><a href="#">Article 2</a></li>

{% endblock %}

答案 1 :(得分:0)

我不认为这是可能的,但您可以尝试使用以下

{% render'bundle:Controller:method' %}

你在这里呈现它

<div class="row">
  <div class="large-12 columns">
    <div class="row">

      <div class="large-4 small-6 columns">
          {{ features }}
      </div>

      <div class="large-4 small-6 columns">
      </div>

      <div class="large-4 small-12 columns">
      </div>

    </div>
  </div>
</div>

我希望这是解决问题的方法