Using PHP code in Smarty tpl FIle

笑着哭i 提交于 2019-11-27 15:04:48

Easy as boiling an egg!

{php}echo "hello!"{/php}

Second link down, for reference.

Edit as of Smarty 3.1:

As of Smarty 3.1 the {php} tags are only available from SmartyBC.

Source: http://www.smarty.net/docs/en/language.function.php.tpl

softnwords

Find the file smarty.class.php in your host directory

Go to smarty.class.php

Edit var $php_handling = SMARTY_PHP_ALLOW;

Save the file in server.

Now you may add php in tpl file as <?php ....code.... ?>

You may have seen the documentation, but you have missed {php}:

The {php} tags allow PHP code to be embedded directly into the template. They will not be escaped, regardless of the $php_handling setting. This is for advanced users only, not normally needed and not recommended.

Emphasis mine, source: http://www.smarty.net/docsv2/en/language.function.php.tpl

Note that putting PHP in template code is the easiest way to shoot yourself in the foot - the main purpose of Smarty is to separate PHP code and HTML templates. In other words, the mere fact of using this tag is a serious red flag; in most cases, it is possible to fix the underlying issue, and avoid PHP inside the template altogether.

Have you tried to enable error reporting?

error_reporting(E_ALL);
ini_set("display_errors", true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!