5

I'm wondering if there's any equivalent to the Yii "params" array I can set on Twig code template level for re-use throughout all my templates using Twig syntax?

  • You could make your own var with a plugin, but you're looking for a native solution, right? – carlcs Feb 10 '15 at 12:19
  • Yeah if there is any. Otherwise I'll go that route or just stick with Globals. But something native would be nice (if possible) from the configuration files. – naboovalley Feb 10 '15 at 12:24
12

Besides saving the variable in a field (e.g. global set) you could also add a "Environment Variable" to your general.php file:

'myGlobalVariable' => 'I am available in all environments'

but you can only access it from Twig, it's not possible to update it:

{{ craft.config.myGlobalVariable }}

If you do need to update from your Twig templates, you should make a custom plugin / add to your Business Logic plugin. There's various options to make a variable available in your Twig templates:

or with a Twig Extention:

With a plugin you have these options to save your value (and update with a custom Twig function):

  • in a Record, update with $record->save()
  • directly in the plugin settings, update with craft()->plugins->savePluginSettings() (→ read more on SE)
  • with a config.php file in the plugin folder, not possible to update with Twig (→ read more on SE)
  • hard code it into the plugin PHP ;)
  • Rock solid answer - as usual! Thank you sir! – naboovalley Feb 10 '15 at 15:52

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.