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 ™.

In this snippet, we will see how to get the block names of a Twig template. To do so, we must use the Twig\Environment service whi...

Twig

Published on 2020-08-08 • Modified on 2020-08-08

 View the code


In this snippet, we will see how to test the type of a variable in a Twig template. We will create a small extension for this purp...

Twig

Published on 2020-06-15 • Modified on 2020-06-15

 View the code


The function is not explode like in PHP. Instead, we can use the split filter (that internally uses the explode or str_split PHP f...

Twig

Published on 2020-02-10 • Modified on 2020-02-10

 View the code


In this snippet, we will see how to manually render checkboxes of a form with Twig. This can be useful when you have special thing...

Symfony

Published on 2020-01-21 • Modified on 2020-01-21

 View the code


Sometimes, it's useful to know the current route name in a Twig template. For example in a layout where you don't "receive" the ro...

Symfony

Published on 2020-01-09 • Modified on 2020-01-09

 View the code


This is another example where the constant Twig function can be helpful. In this case we get the \DateTime::ATOM PHP constant to p...

Twig

Published on 2019-08-18 • Modified on 2019-08-18

 View the code


This is another example where the constant Twig function can be helpful. In this case we get the JSON_PRETTY_PRINT PHP constant to...

Twig

Published on 2019-08-17 • Modified on 2019-08-17

 View the code


Sometimes when using the PHP date function you need to use characters how are usually reserved to be converted in some dynamic val...

Twig

Published on 2019-08-14 • Modified on 2019-08-14

 View the code


Sometimes it's useful to know if a form is valid inside a Twig template. Indeed, when using the form_ helpers like form_error all ...

Symfony

Published on 2019-07-23 • Modified on 2019-11-08

 View the code


Well, it's in fact the Twig helper that I am using on this website to show all the snippets. You already have a native source Twi...

Symfony

Published on 2019-02-08 • Modified on 2019-02-10

 View the code