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.

1 Answer 1


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') }}
  • Very useful in certain cases, indeed. Thanks for sharing, Steve. Commented Nov 18, 2014 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.