php 如何将参数传递给 Symfony2 Twig 块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6068516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i pass parameters to a Symfony2 Twig block?
提问by Adil
I want to generate table headers in a twig block and reuse them across the page, this page has about 5 different tables with roughly the same headers. The block code is such :
我想在树枝块中生成表格标题并在整个页面上重用它们,这个页面有大约 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 %}
The key line in the above code is
上面代码中的关键行是
{% if item.statementType == statementType %}
I want to pass the statementType as parameter where i am rendering the block, like so :
我想将 statementType 作为参数传递给我正在渲染块的地方,如下所示:
{% render block.table_headers with {'statementType': 'INC'} %}
But this doesn't work. I want to keep the block and its rendering in the same file (but different blocks), for conceptual closeness.
但这不起作用。为了概念上的接近,我想将块及其渲染保留在同一个文件中(但不同的块)。
Is it even possible to use blocks like this? I've looked at the Symfony2 docs and couldn't find anything that suggested this could be done, but it seems such an obvious use of blocks to me.
甚至可以使用这样的块吗?我查看了 Symfony2 文档并找不到任何表明可以做到这一点的内容,但对我来说似乎是块的明显使用。
采纳答案by Chris
There is an update to the include tag in Symfony 2.2 which might help you with this. Here's an example of the new tag:
{{ include('FTWGuildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}
Symfony 2.2 中对 include 标签进行了更新,可能会对此有所帮助。以下是新标签的示例:
{{ include('FTWGuildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}
This may be what you need, since it avoids having to do a sub-request to a controller (render
does this) it will be better performing.
这可能是您所需要的,因为它避免了对控制器执行子请求(render
这样做)它会更好地执行。
In my example, I'm including the HTML for a help popover and providing the title and content.
在我的示例中,我包含用于帮助弹出窗口的 HTML 并提供标题和内容。
回答by kxo
Now with Symfony v2+ (3, 4 & 5, since Twig v1.28.0), we can use a custom template on the block()
function using the with
keyword:
现在使用 Symfony v2+(3、4 和 5,从 Twig v1.28.0 开始),我们可以使用关键字在block()
函数上使用自定义模板with
:
{% with {
'myVar1': myValue1,
'myVar2': myValue2
}
%}
{{ block('toolbar', myTemplate) }}
{% endwith %}
Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
提交:https: //github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
回答by alex
{% render block.table_headers with {'statementType': 'INC'} %}
is not recognized by Symfony. You must use:
{% render block.table_headers with {'statementType': 'INC'} %}
Symfony 无法识别。您必须使用:
{% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
回答by inanimatt
Sounds like you want Twig's macros feature. Alternatively write your block as a separate template and use include.
回答by user1041440
Another would be to create a Twig extension, see
另一种方法是创建一个 Twig 扩展,见
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Your Twig function taking care of rendering the header
你的 Twig 函数负责渲染标题
return $this->renderView("MyBundle:Twig:tableHeader.html.twig", array( 'result' => $result));
回答by Chris
For what it's worth to you. Here's an example of how I've rendered blocks of content. This is for a batch app which sends emails, so its a little different than what you're trying, but none the less may be helpful
为了你的价值。这是我如何呈现内容块的示例。这是用于发送电子邮件的批处理应用程序,因此它与您尝试的有点不同,但仍然可能有帮助
$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");
回答by J Ajay
Call any method of class with parameters without key
调用带参数的类的任何方法,无需键
{{ attribute(classname, methodname, [parameter1, parameter2]) }}
{{ attribute(classname, methodname, [parameter1, parameter2]) }}
Call any method of class with parameters with key
使用带键的参数调用类的任何方法
{{ attribute(classname, methodname, {"parameter1" : parameter1, "parameter2" : parameter2]) }}
{{ attribute(classname, methodname, {"parameter1" : parameter1, "parameter2" : parameter2]) }}
Retrieve property of the array/object
检索数组/对象的属性
{{ attribute(array, key) }}
{{ attribute(array, key) }}