Tag
Twig
9 posts tagged Twig.
Posts

09/02/2026
is_granted in Twig: roles and IS_AUTHENTICATED
is_granted takes more than roles, and picking the wrong one gives you a check that quietly passes when it should not. What each attribute tests, why role hierarchy makes ROLE_USER true for your admins, and where the check belongs.

09/02/2026
Rendering a Symfony form field by field in Twig
One call renders the whole form and gives you no say in the markup. Splitting it up is four functions, and the one that matters most is form_rest - the safety net that stops a forgotten field from silently disappearing.

09/02/2026
Twig autoescaping and the raw filter
Autoescaping makes Twig safe by default and makes |raw the fastest way to undo that. The catch is that HTML escaping is only correct in HTML - inside a script tag or an unquoted attribute, the default is the wrong answer.

09/02/2026
The N+1 you wrote in a Twig loop, and its fix
The query looks fine and the template looks fine. Together they issue one query per row, because a lazy association is loaded the moment a template touches it - and templates touch things the controller never did.

07/04/2019
Rendering a Twig template outside a controller
You do not need a controller to render Twig, and you should not pass the container around to get at it. Inject the Environment, and use createTemplate when the template itself comes from the database.

11/30/2015
Extending a parent Twig block with parent()
Define a block in a child template and it replaces the parent's version completely - which is rarely what you wanted for a block holding stylesheets or scripts. parent() puts the inherited content back.

04/28/2015
Show the logged-in user in a Twig template
The snippet everyone has saved uses app.user.username, which throws on every supported Symfony. Here is the version that works, which check to guard it with, and how to greet somebody by name rather than by email address.

04/14/2015
Dynamic field names in Twig with attribute() and ~
Dot notation cannot reach a property whose name you are building in the template. attribute() takes the name as a string, the tilde concatenates it, and `is defined` keeps the loop from throwing on a gap.

04/02/2015
How to show or access cookie values in Symfony Twig views
Strange how obscure it is to find a clear example of how to just access or show the value of a cookie in Twig, so here it is!