11
'dev' => array(
        'siteUrl' => array(
            'de_ch' => 'http://blabla.dev/',
            'en' => 'http://blabla.dev/en/',
        ),
        'environmentVariables' => array(
            'basePath' => 'path',
            'baseUrl' => 'http://url-without-locale-segment.dev/',
        )
    ),

I set a baseUrl inside environmentVariables to be able to grab the url without a locale. But when I enter {{ baseUrl }} in the template it returns an error:

Variable "baseUrl" does not exist

How do I have to write this?

19

You can get to any of your config variables (including custom ones) like this:

{{ craft.config.xyz }}

The environmentVariables array is no different:

{{ craft.config.environmentVariables.baseUrl }}

See the craft.config documentation.

  • Cool. Thank you :) – KSPR Oct 12 '14 at 14:06
  • 2
    Also, if you wanted to use the shorter version of {{ baseUrl }}, you could also add {% set baseUrl = craft.config.environmentVariables.baseUrl %} at the top of your template. – Alex Roper Apr 8 '15 at 8:26
3

If you want to use envVars as you would in the control panel i.e. {baseUrl} but, in your templates, then check out the Filter Environment Variables plugin.

Just apply the envvar filter to your Twig variable:

{% set myVar = '{baseUrl}/contact-us' %}

{{ myVar | envvar }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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