<p>Besides saving the variable in a field (e.g. global set) you could also add a <a href="http://buildwithcraft.com/docs/config-settings#environmentVariables" rel="noreferrer">"Environment Variable"</a> to
your general.php file:</p>

<p><code>'myGlobalVariable' =&gt; 'I am available in all environments'</code></p>

<p>but you can <strong>only access it</strong> from Twig, it's not possible to update it:</p>

<p><code>{{ craft.config.myGlobalVariable }}</code></p>

<p>If you do need to <strong>update from  your Twig templates</strong>, 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:</p>

<ul>
<li>add a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="noreferrer">"Template Variable"</a> to the <code>{{ craft }}</code> global variable</li>
</ul>

<p>or with a Twig Extention:</p>

<ul>
<li>add a new <a href="http://buildwithcraft.com/docs/templating/global-variables" rel="noreferrer">Global Variable</a> using <a href="http://buildwithcraft.com/classreference/etc/templating/twigextensions/CraftTwigExtension#getGlobals-detail" rel="noreferrer"><code>getGlobals()</code></a></li>
<li>add a <a href="http://twig.sensiolabs.org/doc/advanced.html#functions" rel="noreferrer">Twig function</a> with <code>new Twig_SimpleFunction()</code></li>
</ul>

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

<ul>
<li>in a <a href="http://buildwithcraft.com/docs/plugins/records" rel="noreferrer">Record</a>, update with <code>$record-&gt;save()</code></li>
<li>directly in the plugin settings, update with <code>craft()-&gt;plugins-&gt;savePluginSettings()</code> (→ <a href="https://craftcms.stackexchange.com/a/338/125">read more on SE</a>)</li>
<li>with a config.php file in the plugin folder, not possible to update with Twig (→ <a href="https://craftcms.stackexchange.com/a/2000/125">read more on SE</a>)</li>
<li>hard code it into the plugin PHP ;)</li>
</ul>

CC BY-SA 3.0
2015-02-10T15:52:43.107Z
Rock solid answer - as usual! Thank you sir!
naboovalley
CC BY-SA 3.0
2015-02-10T14:20:08.203Z
2015-02-10T14:20:08.203Z
2017-04-13T12:51:00.303Z
carlcs
<p>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?</p>

CC BY-SA 3.0
2015-02-10T12:19:35.457Z
You could make your own var with a plugin, but you're looking for a native solution, right?
carlcs
CC BY-SA 3.0
2015-02-10T12:24:28.053Z
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
<templating>
How can I create global code variables?
# How can I create global code variables?

(Asked by: username_0 on 2015-02-10)

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?

**Tags:** templating
> **Comments:**
> **username_1**: You could make your own var with a plugin, but you're looking for a native solution, right?  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_1 on 2015-02-10. Score: 13 | Accepted answer

Besides saving the variable in a field (e.g. global set) you could also add a ["Environment Variable"](http://buildwithcraft.com/docs/config-settings#environmentVariables) 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:

* add a ["Template Variable"](http://buildwithcraft.com/docs/plugins/variables) to the `{{ craft }}` global variable

or with a Twig Extention:

* add a new [Global Variable](http://buildwithcraft.com/docs/templating/global-variables) using [`getGlobals()`](http://buildwithcraft.com/classreference/etc/templating/twigextensions/CraftTwigExtension#getGlobals-detail)
* add a [Twig function](http://twig.sensiolabs.org/doc/advanced.html#functions) with `new Twig_SimpleFunction()`

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

* in a [Record](http://buildwithcraft.com/docs/plugins/records), update with `$record->save()`
* directly in the plugin settings, update with `craft()->plugins->savePluginSettings()` (→ [read more on SE](https://craftcms.stackexchange.com/a/338/125))
* with a config.php file in the plugin folder, not possible to update with Twig (→ [read more on SE](https://craftcms.stackexchange.com/a/2000/125))
* hard code it into the plugin PHP ;)

> **Comments:**
> **username_0**: Rock solid answer - as usual! Thank you sir!  

---

