Check if the variable is defined in symfony twig

Check if the variable is defined in symfony twig

In twig, specially for the templates being used by many controllers, there might be variables set by one controller but not by others.

Well one way to address could be to have dummy data on each controller and either to check for that dummy value and do something or just to print it

But, symfony has a way to check if the variable has made it to the template or not

{% if some.variable is defined %} what a world {% endif %}

so the usage of defined can rescue us.

composer install-update killed on vagrant machine

Deploying Symfony app on micro instance of aws – Amazon EC2

{% javascript could not be added in symfony assetic

Fatal error: require(): Failed opening required Hydrator in mongodb

bundle does not contain any mapped documents/entities

the requested PHP extension mongo is missing from your system.

One Comment
  1. Maltronic

    defined is a fine solution, although for reference the default() filter can also be used:

    {{ some.variable|default(“”) }}

    or in the case of an if statement:

    {% if some.variable|default(false) %}Only display if some.variable is set and returns true{% endif %}

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*