【问题标题】:Is there a way to include template and hide some of his block有没有办法包含模板并隐藏他的一些块
【发布时间】:2012-05-21 13:15:32
【问题描述】:

也许这个问题可以转为两个问题:

如果我在 .html.twig 中包含一个模板,我可以控制显示或隐藏包含的 twig 中的某些块吗?

我可以从几个树枝布局中扩展一个树枝吗?

【问题讨论】:

    标签: symfony


    【解决方案1】:

    我和一位同事刚刚发现了一件很酷的事情,它与这个问题有某种联系。

    例如,如果您只想在调用renderBlock(Twig 方法)方法而不是在扩展模板时渲染块,则可以使用此技巧。

    {% if false %} {% block subject %} subject_message {% endblock %} {% endif %}

    当您调用renderBlock(如此处-> https://stackoverflow.com/a/7580461/922165)时,将呈现此块,因为该方法不关心其他语句。

    但是,当您扩展时 - 此块将永远不会显示。

    例如,当您有一些其他电子邮件模板正在扩展的基本默认电子邮件模板时,这很有帮助。您可以在块中包装一些默认的电子邮件主题,并且该块不需要显示在其他模板上。

    【讨论】:

      【解决方案2】:

      我不确定这就是你要找的东西,你的描述有点含糊,所以我会坚持这个问题。

      你可以通过中间的模板来解决这个问题。

      您不能直接包含模板并修改其块 AFAIK,但您可以创建 第二个模板扩展第一个模板并删除块,然后 包含第二个模板 在你需要的地方。

      假设这是您最初想要包含的模板:

      <div>
          This is the template you want to include (articleDetails.html.twig)
          {% block article %}
              This is some content you want to delete
          {% endblock article %}
      </div>
      

      您可以尝试创建第二个模板,像这样扩展它:

      {% extends 'articleDetails.html.twig' %}
      
      {# This is template emptyArticle.html.twig, it deletes the article block #}
      {% block article %}
      {% endblock article %}
      

      然后,您可以包含第二个模板 (emptyArticle),而不是包含articleDetails,这样您将获得第一个模板的内容,但不包含文章块的内容。 您可以使用任意数量的块来执行此操作。

      【讨论】:

      • 这是扩展,不包括。 include 表示 --include,如: {% include 'AcmeArticleBundle:Article:articleDetails.html.twig' 和 {'article': article} %} 见 symfony 网站。所以我的问题是“如果我们可以隐藏像扩展模板这样的东西?”
      • 您应该包含第二个模板,请仔细阅读说明。
      • 注意,您可以使用 {{ parent() }} 甚至 {% set parentBlock = parent() %} 来呈现或访问父块内容的字符串,在子块,然后以编程方式对其进行操作。
      【解决方案3】:

      这已经过了这个问题的“到期日期”,但如果其他人可能有类似的问题,我通过在包含的模板中使用 twig 变量解决了这个问题。

      {% set myConditionalBlockToShow %}
          <span>some interesting things {{ withVariable }}</span>
      {% endset %}
      
      {% if myCondition %}
          {{ myConditionalBlockToShow }}
      {% endif %}
      

      【讨论】:

        【解决方案4】:

        您可以在包含中传递变量

        {% include 'template.html' with {'foo': 'bar'} %}
        

        在 template.html 中,您检查 'foo' 的值并根据值显示/隐藏您的块

        或者如果你有更复杂的模板,你可以拆分它们并在控制器中调用它们。

        {% render url('latest_articles', { 'view': 2 }) %}
        
        public function recentArticlesAction($view = 1) {
            // make a database call or other logic
            // to get the "$max" most recent articles
            $articles = ...;
        
            if($view == 2) {
                return $this->render(
                   'AcmeArticleBundle:Article:recentList.html.twig',
                    array('articles' => $articles)
                );
            }
            return $this->render(
                'AcmeArticleBundle:Article:recentList2.html.twig',
                array('articles' => $articles)
            );
        }
        

        http://symfony.com/doc/current/book/templating.html#embedding-controllers

        【讨论】:

          猜你喜欢
          • 2022-01-05
          • 1970-01-01
          • 2015-09-24
          • 2022-11-25
          • 2014-08-16
          • 1970-01-01
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多