Code snippets

You will find here 20 code snippets I find useful so I don't have to Google ™ or Stackoverflow ™ them when I need. There are also code snippets I always forget! 😁 Sometimes, related links to the code will be available. The main difference with other random snippets you will find on the web is that in most cases (for the Symfony related ones) you will be able to run them directly from this website to check the output so you can verify it works as expected ™.

This snippet shows how to display a date interval with Twig. In fact, we can use the diff() function of the DateTime() class and t...

Twig

Published on 2023-04-20 • Modified on 2023-04-20

 View the code


This snippet shows how to display a money amount with Twig. There's a filter for that, and it handles the current locale. The fol...

Twig

Published on 2023-02-25 • Modified on 2023-02-25

 View the code


This snippet shows how to get the name of a Twig template inside a Twig template. Yes, this is inception! Not so hard as there is...

Twig

Published on 2023-02-18 • Modified on 2023-02-18

 View the code


This snippet shows how to get the id of a Symfony form inside a Twig template. This is useful when wanting to give a particular at...

Symfony

Published on 2022-08-06 • Modified on 2022-08-06

 View the code


This snippet shows how to access an object property or array index with Twig dynamically. We can use the same attribute() function...

Twig

Published on 2022-06-20 • Modified on 2022-06-20

 View the code


In this snippet, we see how to create a unit test for a Twig template. This template contains a simple if tag like this: {% if cas...

Twig

Published on 2021-07-02 • Modified on 2021-07-02

 View the code


In this snippet, we see how to get the value of a Symfony form field inside a Twig template. We can access it with the help of the...

Symfony

Published on 2021-06-05 • Modified on 2021-06-05

 View the code


In this snippet, we see how to test if a block in a Twig template exists. It's sometimes helpful to know if one of the parent temp...

Twig

Published on 2021-05-22 • Modified on 2021-05-22

 View the code


In this snippet, we see how to get the current environment in a Twig template with Symfony. This is simple to do thanks to the twi...

Twig

Published on 2021-05-05 • Modified on 2021-05-05

 View the code


In this snippet, we will see how to access the logged user in a Twig template. This can be you through the app Twig global variabl...

Twig

Published on 2021-03-27 • Modified on 2021-03-27

 View the code