For Craft CMS 3 checkout Craft Dynamic Variables
Replace environment variable and global strings.
To install replacevar, follow these steps:
- Download & unzip the file and place the
replacevar
directory into yourcraft/plugins
directory - -OR- do a
git clone https://github.com/zizther/replacevar.git
directly into yourcraft/plugins
folder. You can then update it withgit pull
- Install plugin in the Craft Control Panel under Settings > Plugins
- The plugin folder should be named
replacevar
for Craft to see it. GitHub recently started appending-master
(the branch name) to the name of the folder for zip file downloads.
Include reference tokens for environment variables and/or globals in your field values. To make a value parseable, enclose it with single-brackets and no spaces.
Lorem ipsum {myEnvironmentVariable} sit amet. Ut enim ad {globalSet.fieldName}, quis nostrud exercitation.
The token name or global should be an exact match of an existing environment variable. If no match is found, the token will not be parsed out.
Just apply the replacevar
or rv
filter to your Twig variable:
{{ entry.fieldName | replacevar }}
This also works if the token is part of a hard-coded string:
{{ '{baseUrl}/contact-us' | rv }}
That's it! Any matching environment variables defined in your config file or globals will be parsed
It can also be called within other plugins or in Element API config
craft()->replacevar->replacevar($entry->fieldName)
Within your config/general.php
file, they are strings which can be defined differently for each of your environments.
Learn more about environment variables...
- PHP 5.4+
- Craft 2.4+
Thanks to @carlcs for his help.