Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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

1 Answer 1

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 at 17:15
1  
Little known, but very handy feature! –  Brad Bell Jul 3 at 17:17

Your Answer

 
discard

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.