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 :
我想在一个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 %}
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文档,找不到任何暗示可以做到的事情,但对我来说这似乎是一个明显的块使用。
6 个解决方案
#1
12
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.
这可能是你需要的,因为它避免了必须对控制器做一个子请求(渲染就是这样),它会更好地执行。
In my example, I'm including the HTML for a help popover and providing the title and content.
在我的示例中,我将HTML包含在帮助弹出框中并提供标题和内容。
#2
7
{% render block.table_headers with {'statementType': 'INC'} %}
is not recognized by Symfony. You must use:
Symfony无法识别{%render block.table_headers with {'statementType':'INC'}%}。你必须使用:
{% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
#3
5
Now, we can use with syntax:
现在,我们可以使用语法:
{% with {
'myVar1': myValue1,
'myVar2': myValue2
}
%}
{{ block('toolbar', myTemplate) }}
{% endwith %}
Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
提交:https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
#4
4
Sounds like you want Twig's macros feature. Alternatively write your block as a separate template and use include.
听起来你想要Twig的宏功能。或者将您的块写为单独的模板并使用include。
#5
0
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));
#6
0
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");
#1
12
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.
这可能是你需要的,因为它避免了必须对控制器做一个子请求(渲染就是这样),它会更好地执行。
In my example, I'm including the HTML for a help popover and providing the title and content.
在我的示例中,我将HTML包含在帮助弹出框中并提供标题和内容。
#2
7
{% render block.table_headers with {'statementType': 'INC'} %}
is not recognized by Symfony. You must use:
Symfony无法识别{%render block.table_headers with {'statementType':'INC'}%}。你必须使用:
{% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
#3
5
Now, we can use with syntax:
现在,我们可以使用语法:
{% with {
'myVar1': myValue1,
'myVar2': myValue2
}
%}
{{ block('toolbar', myTemplate) }}
{% endwith %}
Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
提交:https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
#4
4
Sounds like you want Twig's macros feature. Alternatively write your block as a separate template and use include.
听起来你想要Twig的宏功能。或者将您的块写为单独的模板并使用include。
#5
0
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));
#6
0
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");