Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to use strait PHP in our templates along side of Twig and or instead of Twig?

If so how does one go about it?

share|improve this question
up vote 19 down vote accepted

No this is not possible.

If you really need PHP, you can write a plugin with one of the following:

  • a Template Variable class, which can be accessed from your templates via craft.myPluginHandle
  • a Twig extension, which can add its own global filters, functions, or tags, etc.
  • a controller, which can execute PHP before calling $this->renderTemplate(...)
  • a template hook, which allows PHP code to access and modify the current template variable context, wherever the {% hook %} tag is placed.

Why Twig anyway?

The question to use php in templates comes up every once in a while, but there are good reasons for not using it.

For example if Craft changes its code you don't have to worry about your templates. The upcoming update of the Yii framework would force you to rewrite all your templates if they were written in php.

There is also a great article from Fabien Potencier on this topic -- tl;dr php is too verbose for templating.

share|improve this answer
Thanks Brad, I didn't even knew there were template hooks. – Victor In Jul 3 '14 at 17:15
Little known, but very handy feature! – Brad Bell Jul 3 '14 at 17:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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