4

I was looking for a way to set arbitrary PHP constants in the index.php that could then be accessed within templates.

This is particularly useful if you have more than one index/domain pulling content from a core Craft install as the additional domains won't have any configuration files to use, just index.php.

5

Hat-tip to Brad for finding this answer.

This can be achieved with something like the following in your index.php:

define('BRADS_FAVORITE_HOBBY', "Drinking");

Thanks to the power to Twig, you can then access this in your templates using:

{{ constant('BRADS_FAVORITE_HOBBY') }}
| improve this answer | |
  • Very useful in certain cases, indeed. Thanks for sharing, Steve. – Jérôme Coupé Nov 18 '14 at 10:48

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.