How can i pass parameters to a Symfony2 Twig block?
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 {开发者_C百科'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.
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:
{% with {
'myVar1': myValue1,
'myVar2': myValue2
}
%}
{{ block('toolbar', myTemplate) }}
{% endwith %}
Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191
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.
{% render block.table_headers with {'statementType': 'INC'} %}
is not recognized by Symfony. You must use:
{% render "yourBundle:controleur:action" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
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:
{% set foo = 'bar' %}
{{ block('another_block') }}
In child template:
{% block another_block %}
{{ foo }}
{% endblock %}
Prints:
bar
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
return $this->renderView("MyBundle:Twig:tableHeader.html.twig", array( 'result' => $result));
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");
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) }}
You can combine block
with the with
tag.
{%- with {'statementType': 'INC'} -%}
{{- block('table_headers') -}}
{%- endwith -%}
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
{% block test %}
{{ dump(test_param|default('no value')) }}
{% endblock %}
Child template
{% block test %}
{% with {
test_param: 123
} %}
{{ parent() }}
{% endwith %}
Some additional content
{% endblock %}
精彩评论