103

Is there a way to get the name of the current environment in a Twig template? I'd like to set some CSS style values depending on it.

3 Answers 3

183

http://symfony.com/doc/current/templating/app_variable.html:

<p>Application Environment: {{ app.environment }}</p>
2
  • It slipped my eye, sorry about that... Thank you for pointing out! Apr 17, 2012 at 18:14
  • 2
    It would have been helpful to provide the actual answer with the link.
    – afilina
    Jan 14, 2014 at 22:36
46

Use

app.environment

e.g.

{% extends app.environment == 'dev' ? "::dev_layout.html.twig" : "::layout.html.twig" %}
24

Or you can use

app.debug

This returns true if debug is enabled. This is usually the case in the dev environment, however debug can be enabled in any of the environments... prod, test, dev, etc....

5
  • 6
    app.debug returns true only when debugging is enabled. Although enabling debug mode in the production environment is generally a bad idea, it's not impossible it is turned on. Jul 30, 2013 at 14:52
  • 3
    Debug can be enabled or disabled for any environment. Also, there are more than just two environments possible (I usually use 3 or 4), so a boolean is of little help.
    – afilina
    Jan 14, 2014 at 22:38
  • 2
    Do not use this, use app. environment Mar 22, 2016 at 16:08
  • 2
    Do not use is not helpful. Explain why.
    – RichieHH
    May 18, 2017 at 11:26
  • 1
    Although this is not the proper answer to the OP, this is more useful Feb 5, 2018 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.