Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Twig Mind Tricks

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

I've got 2 more bonuses from Twig. In every template, you have access to a variable called app. This has a bunch of useful things on it, like the request, the security context, the User object, and the session:

{# src/Yoda/EventBundle/Resources/views/Event/index.html.twig #}
{# ... #}

{% block body %}
    {# some examples - remove these after you try them #}
    {{ app.session.get('some_session_key') }}
    {{ app.request.host }}

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

It's actually an object called GlobalVariables, which you can check out yourself. So when you need one of these things, remember app!

Tip

Remove this code after trying it out - it's just an example of how you can access the request and session data - it doesn't add anything real to our project.

The block Twig Function

Next, head to our base template. Right now, the title tag is boring: I can either replace it entirely in a child template or use the default:

<title>{% block title %}Welcome!{% endblock %}</title>

Let's make this better by adding a little suffix whenever the page title is overridden. This shows off the block function, which gives us the current value of a block:

<title>
    {% if block('title') %}
        {{ block('title') }} | Starwars Events
    {% else %}
        Events from a Galaxy, far far away
    {% endif %}
</title>

Refresh the events page to see it in action.

Twig Whitespace Control

But if you view the source, you'll see that we've got a lot of whitespace around the title tag. That's probably ok, but let's fix it anyways. By adding a dash to any Twig tag, all the whitespace on that side of the tag is removed. The end result is a title tag, with no whitespace at all:

<title>
    {%- if block('title') -%}
        {{ block('title') }} | Starwars Events
    {%- else -%}
        Events from a Galaxy, far far away
    {%- endif -%}
</title>

Wow! Congrats on finishing the first episode! You're well on your way with Symfony, so keep going with Episode 2 and start practicing on a project.

Seeya next time!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

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

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0" // v2.2.0
    }
}
userVoice