I have 4 main template folders, under the /craft folder (each template folder works for another subdomain).

Now, I do have per template folder, a few variables that I like to have shared amongst all templates underneath it.

Such as:

{% set projectsGroup = "projectsWeather" %}
{% set sliderGroup = "sliderWeather" %}
{% set faqGroup = "faqWeather" %}

Which are different, per template folder.

Now, when I put these variables at the top of my master _layout file, this gets shared nicely amongst all templates -- that is, for those that use this _layout file.

I do however also have a few json generation template files, that can not use the _layout file.

My question now is how to elegantly assign these variables, and do this only once.

I can not stick them in a separate file and just use "include" or "extend", because then _layout either ignores or complains.

Any ideas?


Add them to the environmentVariables section of your config file, and retrieve them using the craft.config template tag.

For example:

// Config 
"environmentVariables" => [
    "color" => "blue",

// Template
{{ craft.config.environmentVariables.color }}
  • Thanks for this. I was hoping for a more isolated solution (per template folder), but this will do fine as well.
    – tom
    Apr 15 '17 at 22:24
  • You can create nested arrays in the environment variables array, to "scope" your variables.. Apr 15 '17 at 22:26

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.