Sitemap

Twig Tips

1 min readNov 6, 2016

--

Generating flexible urls

Twig’s path function generates urls based on route names you defined. It even takes parameters! For example, let’s say that we passed “entity” to our template file from our controller:

<a href=”{{path(event_edit,{'id': entity.id})}}”

How to display dates

It is as easy as using plain old php date() function! Let’s see an example.

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

You actually can validate conditions with twig!

Get Fatih Yavuz’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, lets check if title block is overwritten. If so, we’ll add append it with ‘Star Wars Events!’. If not.. I’m pretty sure that you can understand that part :)

{% if block('title') %}{{block('title')}} | Star Wars Events!{% else %}Event from a galaxy far far away!{% endif %}

So, basically you can check whether the block part is used or not, accordingly take an action! Isn’t that nice? :)

Did you know that, you can use global variables in twig?

Now just go head and put

{{ app.request.host }}

in your twig file. What do you think it will display?

Exactly! You guessed it correct. It is exactly the same as this php variable.

$_SERVER['HTTP_HOST']

That’s it for now. I will keep adding things, as I proceed with Symfony!

See you around!

--

--