如何将PHP代码添加到.tpl文件

如何将PHP代码添加到.tpl文件,php,smarty,Php,Smarty,我需要显示一些从php文件到.tpl文件的外部数据。为此,我想将php文件包含到.tpl文件中。我已经尝试使用下面的代码向tpl显示php文件内容 {php} include('custom_code.php'); {/php} 但页面上的输出是include('custom_code.php')smarty主页上有一个最佳实践指南#1是不要嵌入PHP 试试这个:{include_php file=“/path/to/somefile.php”} 但请注意: {include_php} is

我需要显示一些从php文件到.tpl文件的外部数据。为此,我想将php文件包含到.tpl文件中。我已经尝试使用下面的代码向tpl显示php文件内容

{php} include('custom_code.php'); {/php}

但页面上的输出是
include('custom_code.php')

smarty主页上有一个最佳实践指南#1是不要嵌入PHP

试试这个:
{include_php file=“/path/to/somefile.php”}

但请注意:

{include_php} is deprecated from Smarty, use registered plugins 
to properly insulate presentation from the application code. 
As of Smarty 3.1 the {include_php} tags are only available 
from SmartyBC.

所以,最好的方法是编写一个smarty插件,正如rodneyrehm所解释的那样,您不应该将PHP代码添加到模板中。这将破坏模板的整个概念


您必须将PHP代码添加到控制器,而不是模板

{php}
已被弃用。看一看

将以下内容放入
../plugins/function.yourplugin.php

<?php
function smarty_function_yourplugin(array $params, Smarty_Template_Instance) {
    include 'your_other_file.php';
}

您的Smarty分隔符是
{}
?如果他们是
{{}
,那该怎么办?感谢rodneyrehm提供的解决方案,我将尝试您的解决方案。
{yourplugin}