Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Including other Templates

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

Using a base layout is very common, and we've just mastered that! Now suppose that we have a small "sales" banner that we want to include on both the homepage and the contact page, because penguins care about saving some krill too. If we wanted it on every page, then we should put it in our layout, but pretend for now that we only need it on these 2 pages.

Using the include Function

To avoid duplication, create a new template that will hold the sales banner. The filename doesn't matter, but I often prefix these files with an underscore to show that they only contain a small page fragment, not a whole page:

{# templates/_banner.twig #}

<div class="well">
    <h3>Save some Krill!</h3>

    <p>Sale in summer suits all this weekend!</p>
</div>

To include this on the homepage we can use the include() function. We use this in a say something syntax because include() renders the other template, and we want to print its content:

{# templates/homepage.twig #}

{% block body %}
    {{ include('_banner.twig') }}

    {# ... #}
{% endblock %}

Let's add the same code to contact.twig and refresh to make sure that our big sales banner is showing up. Cool!

Passing Variables

We can also access our variables from within the included template. Since both pages have a pageData variable, we can use it from within the included template:

{# templates/_banner.twig #}

<div class="well">
    <h3>Save some Krill!</h3>

    <p>Sale in summer suits all this weekend! {{ pageData.title }}</p>
</div>

You can also pass extra variables to the template. The include() function takes two arguments: the name of the template to include and a collection of additional variables to pass. These variables are a key-value list of names and their values.

In Twig, a key-value array is called a "hash", and uses a syntax that's just like JavaScript or JSON (i.e. {"foo": "bar"}). Let's pass a backgroundColor variable into the template and use it.

{# templates/homepage.twig #}

{% block body %}
    {{ include('_banner.twig', { 'backgroundColor': 'violet' }) }}

    {# ... #}
{% endblock %}
{# templates/_banner.twig #}

<div class="well" style="background-color: {{ backgroundColor }};">
    <h3>Save some Krill!</h3>

    <p>Sale in summer suits all this weekend! {{ pageData.title }}</p>
</div>

When we refresh, we see a beautiful purple background.

Leave a comment!

4
Login or Register to join the conversation
Default user avatar

Hello, please tell me
Challenge 2

Why in '_thirdTemplate.twig'

Set the value of a variable with set and use the default filter for the same variable?

In which case does the default filter work?

Reply

Hey Nina,

Tricky question, right? :) From the Twig docs:
> The default filter returns the passed default value if the value is undefined or empty, otherwise the value of the variable.

So it means if "number" variable does not exists at all or is empty, i.e. equal to an empty string, null, false - the value you passed to default filter will be used.

Cheers!

Reply
Default user avatar
Default user avatar Oliver Williams | posted 5 years ago

The syntax in the documentation is different from this. Has it changed? I had trouble getting this to work.

Reply

Hi Oliver!

Hmm, no it hasn't changed. You're probably referring to this syntax: {% include ... %} (http://twig.sensiolabs.org/.... That's an older syntax and the newer one - {{ include() }} is what you should use now. What error are you getting? Let's see if we can get it working :)

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial uses Twig 1. There are newer versions of Twig, but they don't contain significant differences. So, Twig on!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": []
}
userVoice