【问题标题】:How can i pass parameters to a Symfony2 Twig block?如何将参数传递给 Symfony2 Twig 块?
【发布时间】:2011-08-29 10:30:39
【问题描述】:

我想在一个 twig 块中生成表头并在整个页面中重复使用它们,这个页面有大约 5 个不同的表,它们的表头大致相同。块代码是这样的:

{% block table_headers %}
    <th>Fiscal Year</th>
    <th>End Date</th>
    <th>Period Length</th>
    {% for item in result.FinancialStatements.COAMap.mapItem %}
        {% if item.statementType == statementType %}
            <th>{{ item._ }} ({{ item.coaItem }})</th>
        {% endif %}
    {% endfor %} 
{% endblock %}

上面代码中的关键行是

{% if item.statementType == statementType %}

我想将 statementType 作为参数传递给我渲染块的位置,如下所示:

{% render block.table_headers with {'statementType': 'INC'} %}

但这不起作用。为了概念上的接近,我想将块及其渲染保留在同一个文件(但不同的块)中。

甚至可以使用这样的块吗?我查看了 Symfony2 文档,找不到任何建议可以这样做的信息,但对我来说,这似乎是对块的明显使用。

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

    现在使用 Symfony v2+(3、4 和 5,自 Twig v1.28.0 起),我们可以使用 with 关键字在 block() 函数上使用自定义模板:

    {% with {
                'myVar1': myValue1,
                'myVar2': myValue2
            }
    %}
            {{ block('toolbar', myTemplate) }}
    {% endwith %}
    

    提交:https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191

    【讨论】:

    • 这对我来说很好,特别是因为with 不会覆盖超出其范围的变量。
    【解决方案2】:

    Symfony 2.2 中包含标签的更新可能会对您有所帮助。以下是新标签的示例: {{ include('FTWGuildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}

    这可能是您需要的,因为它避免了对控制器执行子请求(render 这样做)它会更好地执行。

    在我的示例中,我包含了帮助弹出窗口的 HTML 并提供标题和内容。

    【讨论】:

    • 我希望这个功能在我 2011 年第一次发布问题时就存在。+1
    • 谢谢 - 这正是我想要的,这意味着我不必在 routing.yml 中添加任何路由。
    • 在 Twig 的更高版本中已过时,请使用在参数和路由之间使用 'with' 而不是逗号的替代方案。
    【解决方案3】:

    {% render block.table_headers with {'statementType': 'INC'} %} 无法被 Symfony 识别。您必须使用:

    {% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
    

    【讨论】:

    • 这嵌入了一个控制器,这绝对是 OP 所要求的。
    【解决方案4】:

    听起来你想要 Twig 的 macros feature。或者将您的块编写为单独的模板并使用include。

    【讨论】:

    • 我最终使用了带有全局参数的宏和块。 include 会将属于一起的逻辑拆分为多个文件,我避免这样做。
    【解决方案5】:

    当使用block function 时,子模板可以访问父变量:

    {% set foo = 'bar' %}
    {{ block('another_block') }}
    

    在子模板中:

    {% block another_block %}
        {{ foo }}
    {% endblock %}
    

    打印:

    bar
    

    【讨论】:

      【解决方案6】:

      另一种方法是创建 Twig 扩展,请参阅

      http://symfony.com/doc/current/cookbook/templating/twig_extension.html

      你的 Twig 函数负责渲染标题

      return $this->renderView("MyBundle:Twig:tableHeader.html.twig", array( 'result' => $result));
      

      【讨论】:

        【解决方案7】:

        因为它对你有价值。这是我如何渲染内容块的示例。这是用于发送电子邮件的批处理应用程序,因此它与您尝试的有点不同,但仍然可能有帮助

                $templateContent = $this->getContainer()->get('twig')->loadTemplate('FTWGuildBundle:AuctionNotification:notificationEmail.html.twig');
                $body = $templateContent->renderBlock('body', array('siteDomain' => $siteClient->getSiteDomain(), 'staticContentDomain' => $siteClient->getStaticContentDomain(), 'batch' => $batch->getNotifications(), 'auction_notification_lockout_period' => $this->getContainer()->getParameter('auction_notification_lockout_period')));
                $subject = ($templateContent->hasBlock("subject")
                    ? $templateContent->renderBlock("subject", array('batchSize' => $batch->getSize(), 'batch' => $batch))
                    : "Auction House Notifications");
        

        【讨论】:

          【解决方案8】:

          调用类的任何方法,参数不带key

          {{ attribute(classname, methodname, [parameter1, parameter2]) }}

          使用key调用任何类的方法

          {{ attribute(classname, methodname, {"parameter1" : parameter1, "parameter2" : parameter2]) }}

          检索数组/对象的属性

          {{ attribute(array, key) }}

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-09-27
            • 1970-01-01
            • 2021-03-22
            • 1970-01-01
            • 2015-04-07
            • 2011-05-30
            • 2017-12-01
            • 1970-01-01
            相关资源
            最近更新 更多