Adil
Adil

Reputation: 2112

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 {'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.

Upvotes: 28

Views: 57990

Answers (10)

Alex Bacart
Alex Bacart

Reputation: 126

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 %}

Result

enter image description here

Upvotes: 0

Werrolf
Werrolf

Reputation: 1

You can combine block with the with tag.

{%- with {'statementType': 'INC'} -%}
  {{- block('table_headers') -}}
{%- endwith -%}

Upvotes: 0

Mike Doe
Mike Doe

Reputation: 17634

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

Upvotes: 6

David DIVERRES
David DIVERRES

Reputation: 1921

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

Upvotes: 21

J Ajay
J Ajay

Reputation: 327

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) }}

Upvotes: 0

inanimatt
inanimatt

Reputation: 702

Sounds like you want Twig's macros feature. Alternatively write your block as a separate template and use include.

Upvotes: 7

Chris
Chris

Reputation: 754

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");

Upvotes: 0

Chris
Chris

Reputation: 754

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.

Upvotes: 13

user1041440
user1041440

Reputation: 211

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));

Upvotes: 0

alex
alex

Reputation: 81

{% render block.table_headers with {'statementType': 'INC'} %} is not recognized by Symfony. You must use:

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

Upvotes: 8

Related Questions