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

Friendly Links and Dates in Twig

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

Let's look at a couple of things that are hiding in those new template files I created. Open up index.html.twig and notice the for tag, which loops over the entities variable we're passing to the template:

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

{% for entity in entities %}
    <article>...</article>
{% endfor %}

You're going to loop over a lot of things in your Twig days, so take a close look at the syntax.

Generating URLs

Further down, check out how we link to the "show" page for each event. Instead of hardcoding the URL, Symfony generates the URL based on the route:

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

<a href="{{ path('event_show', {'id': entity.id}) }}">
    {{ entity.name }}
</a>

Look at the event routes that were generated earlier and find one called event_show:

# src/Yoda/EventBundle/Resources/config/routing/event.yml
# ...

event_show:
    pattern:    /{id}/show
    defaults:   { _controller: "EventBundle:Event:show" }

To generate a URL in Twig, we use the Twig path function. Its first argument is the name of the route we're linking to, like event_show. The second is an array of wildcards in the route. We pass in the actual value we want for the id wildcard.

<a href="{{ path('event_show', {'id': entity.id}) }}">
    {{ entity.name }}
</a>

In the browser, you can see how each link generates almost the same URL, but with a different id portion.

Rendering Dates in a Template

One more hidden trick. The Event class's time field is represented internally by a PHP DateTime object. We saw this in our play.php file when we created an event. To actually render that as a string, we use Twig's date filter:

<dd>
    {{ entity.time|date('g:ia / l M j, Y') }}
</dd>

It transforms dates into another format and the string we pass to it is from PHP's good ol' fashioned date function formats.

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