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?

naboovalley
  • 2,844
  • 1
  • 16
  • 26
  • 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

1 Answers1

13

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 ;)
carlcs
  • 36,220
  • 5
  • 62
  • 139