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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityNo this is not possible.
If you really need PHP, you can write a plugin with one of the following:
craft.myPluginHandle
$this->renderTemplate(...)
{% 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.
In craft 3 it is actually possible.
{{ craft.app.view.evaluateDynamicContent('phpinfo();') }}
As you can see above, php code is passed as string to evaluateDynamicContent function. On inside, evaluateDynamicContent is just running eval
function.
Now, even if you can do this, doesn't mean that you should. Before doing this, consider if you have other alternatives for doing what you want to achieve. Using php code inside template can run you into all kind of unpredicted security issues.