Is there a way to check what environment craft is running on - ie, dev or not dev - via the template.
For example:
{%if env is 'dev'%}
hello dev
{% else %}
hello world
{% endif %}
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIs there a way to check what environment craft is running on - ie, dev or not dev - via the template.
For example:
{%if env is 'dev'%}
hello dev
{% else %}
hello world
{% endif %}
When developing locally on Craft you would most likely have devMode => true
set in your config file and set to false for your production config.
app/config/general.php
return array(
'mydevdomain.dev' => array(
'devMode' => true
),
'mylivedomain.com' => array(
'devMode' => false
)
);
So you could test whether craft is in dev mode and respond to that:
{% if craft.config.devMode %}
You are in dev mode
{% else %}
You are not in devmode
{% endif %}
'cache' => false
→ {% if craft.config.cache %}
)
– carlcs
Aug 12 '14 at 10:32
In Craft 3 you can get the env using the getenv()
function:
{% set env = getenv('ENVIRONMENT') %}
{% if env == 'dev' %}
hello dev
{% else %}
hello world
{% endif %}
Or via the app’s config service:
{% set env = craft.app.config.env %}
There's a bit more information about the .env file in the docs for Environmental Configuration.
You can set a custom "environment label" variable inside config/general.php:
return array(
// ALL ENVIRONMENTS
'*' => array(
),
// LOCAL
'mysite.local' => array(
'env' => 'local'
),
// DEV
'dev.mysite.com' => array(
'env' => 'dev'
),
// PROD
'www.mysite.com' => array(
'env' => 'prod'
)
);
Then use the craft.config
variable to output the env
variable for the current environment:
{% if craft.config.env == 'dev' %}
hello dev
{% else %}
hello world
{% endif %}