在 Smarty tpl 文件中使用 PHP 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7446851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP code in Smarty tpl FIle
提问by jawad waheed
I am new to smarty and I want to use php code in template file i-e tpl file. I have seen the documentation and searched on google but could not find how to use php code they say we need to configure smarty to allow php execution but could not find how to do it.
我是 smarty 的新手,我想在模板文件中使用 php 代码,即 tpl 文件。我看过文档并在 google 上搜索过,但找不到如何使用 php 代码,他们说我们需要配置 smarty 以允许 php 执行,但找不到如何使用。
Kindly help me in this regard. Thanks
请在这方面帮助我。谢谢
回答by Prisoner
Easy as boiling an egg!
像煮鸡蛋一样简单!
{php}echo "hello!"{/php}
Second link down, for reference.
第二个链接下来,供参考。
Edit as of Smarty 3.1:
从 Smarty 3.1 开始编辑:
As of Smarty 3.1 the {php} tags are only available from SmartyBC.
从 Smarty 3.1 开始, {php} 标签只能从SmartyBC 获得。
Source: http://www.smarty.net/docs/en/language.function.php.tpl
回答by softnwords
Find the file smarty.class.php
in your host directory
smarty.class.php
在您的主机目录中找到该文件
Go to smarty.class.php
去 smarty.class.php
Edit var $php_handling = SMARTY_PHP_ALLOW
;
编辑var $php_handling = SMARTY_PHP_ALLOW
;
Save the file in server.
将文件保存在服务器中。
Now you may add php in tpl file as <?php ....code.... ?>
现在你可以在 tpl 文件中添加 php 作为 <?php ....code.... ?>
回答by Piskvor left the building
You may have seen the documentation, but you have missed {php}
:
您可能已经看过文档,但您错过了{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.
{php} 标签允许将 PHP 代码直接嵌入到模板中。无论 $php_handling 设置如何,它们都不会被转义。这仅适用于高级用户,通常不需要也不推荐。
Emphasis mine, source: http://www.smarty.net/docsv2/en/language.function.php.tpl
强调我的,来源: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.
请注意,将 PHP 放在模板代码中是最简单的方法——Smarty 的主要目的是将 PHP 代码和 HTML 模板分开。换句话说,仅仅使用这个标签的事实就是一个严重的危险信号;在大多数情况下,可以解决根本问题,并完全避免在模板中使用 PHP。
回答by Olli
Have you tried to enable error reporting?
您是否尝试过启用错误报告?
error_reporting(E_ALL);
ini_set("display_errors", true);