简体   繁体   English

在Smarty tpl FIle中使用PHP代码

[英]Using PHP code in Smarty tpl FIle

I am new to smarty and I want to use php code in template file ie tpl file. 我是聪明的新手,我想在模板文件即tpl文件中使用php代码。 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. 我已经看过文档并在谷歌搜索,但无法找到如何使用PHP代码他们说我们需要配置smarty允许PHP执行但无法找到如何做到这一点。

Kindly help me in this regard. 请在这方面帮助我。 Thanks 谢谢

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 资料来源: http//www.smarty.net/docs/en/language.function.php.tpl

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.... ?>

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. {php}标签允许PHP代码直接嵌入到模板中。 They will not be escaped, regardless of the $php_handling setting. 无论$ php_handling设置如何,它们都不会被转义。 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 强调我的,来源: 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. 请注意,将PHP放在模板代码中是最简单的方法 - Smarty的主要目的是分离PHP代码和HTML模板。 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。

Have you tried to enable error reporting? 您是否尝试启用错误报告?

error_reporting(E_ALL);
ini_set("display_errors", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM