我如何将参数传递给 Symfony2 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 文档,但找不到任何表明可以完成此操作的内容,但对我来说,这似乎是块的明显使用。
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 :
{% 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 :
{% 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
现在,使用 Symfony v2+(3、4 和 5,自 Twig v1.28.0 起),我们可以使用
with
关键字在block()
函数上使用自定义模板:提交: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a01219 1
Now with Symfony v2+ (3, 4 & 5, since Twig v1.28.0), we can use a custom template on the
block()
function using thewith
keyword:Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
Symfony 2.2 中的 include 标签有一个更新,可能会帮助您解决这个问题。以下是新标签的示例:
{{ include('FTWGildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}
这可能就是您所需要的,因为它避免了向控制器发出子请求(
render
就是这样做的),它的性能会更好。在我的示例中,我包含了帮助弹出窗口的 HTML 并提供了标题和内容。
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}) }}
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.In my example, I'm including the HTML for a help popover and providing the title and content.
Symfony 无法识别
{% render block.table_headers with {'statementType': 'INC'} %}
。您必须使用:{% render block.table_headers with {'statementType': 'INC'} %}
is not recognized by Symfony. You must use:听起来您想要 Twig 的宏功能。或者将您的块编写为单独的模板并使用 include。
Sounds like you want Twig's macros feature. Alternatively write your block as a separate template and use include.
当使用块函数时,子模板可以访问父变量:
在子模板中:
打印:
When using the block function, child template has access to parent vars:
In child template:
Prints:
另一种方法是创建一个 Twig 扩展,请参阅
http://symfony.com/doc /current/cookbook/templatating/twig_extension.html
你的 Twig 函数负责渲染标题
Another would be to create a Twig extension, see
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Your Twig function taking care of rendering the header
对于你来说它的价值是什么。这是我如何呈现内容块的示例。这是用于发送电子邮件的批处理应用程序,因此它与您正在尝试的有点不同,但仍然可能有帮助
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
调用不带参数的类的任意方法
{{ attribute(classname, methodname, [parameter1,parameter2]) }}
调用带参数的类的任意方法
{{ attribute(classname, methodname, {"parameter1" :parameter1, "parameter2" :parameter2]) }}
检索数组/对象的属性
{{ attribute(array, key) }}
Call any method of class with parameters without key
{{ attribute(classname, methodname, [parameter1, parameter2]) }}
Call any method of class with parameters with key
{{ attribute(classname, methodname, {"parameter1" : parameter1, "parameter2" : parameter2]) }}
Retrieve property of the array/object
{{ attribute(array, key) }}
您可以将
block
与结合起来带有
标签。You can combine
block
with thewith
tag.Twig 不仅允许使用子模板中的一些数据来渲染块,还可以向渲染的块添加一些内容。希望它能帮助某人。
基本模板
子模板
结果
Twig allows not only to render block with some data from child template but also add some content to the rendered block. Hope it'll help someone.
Base template
Child template
Result