Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Twig with multiple parameters

Is there a way to include a Twig-template with more than one parameter?

I tried this, but it didn't work:

The following Twig is rendered by my Symfony Controller:

{% for object in objects %}
        {% if object.type == "simple" %}
               {% include 'BBLWebBundle:content:simple.html.twig' 
                with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}

        {% elseif object.type == "mp3" %}
                {% include 'BBLWebBundle:content:mp3.html.twig'
                with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}

        {% elseif object.type == "video" %}
                {% include 'BBLWebBundle:content:video.html.twig' 
                with [{'picture': object.picture}, {'link': object.link}, {'name': object.name}, {'info': object.info}] %}
        {% endif %}
{% endfor %}    

The Controller also passes some parameters (this is just some Dummy-Data):

    $objects['ob1']['type'] = "simple";
    $objects['ob1']['picture'] = "this is a picture";
    $objects['ob1']['link'] = "#";
    $objects['ob1']['info'] = "Oh wooow some Info";
    $objects['ob1']['name'] = "Potato";
    return $this->render('BBLWebBundle:Base:content.html.twig',
            array('objects' => $objects, 'title' => "Im very cool Title"));

This is one Twig-template which should be included:

<div>{{ picture }}</div> 
<div><a href="{{ link }}"> <h3>{{ name }}</h3></a><br />{{ info }}<br /></div>
like image 839
Simeon Avatar asked Apr 15 '13 16:04

Simeon


1 Answers

It's easier than you think:

{% include 'BBLWebBundle:content:simple.html.twig' 
     with {'picture': object.picture, 'link': object.link, 'name': object.name, 'info': object.info} %}
like image 124
akluth Avatar answered Oct 16 '22 22:10

akluth