Smarty:如何在模板中包含php?

时间:2011-07-08 13:52:17

标签: php smarty

我已经尝试了{include_php file="phpfile.php"}{php}标记,但都导致了弃用错误。你能不能再在Smarty做这个了吗?我在文档中找不到任何内容。

3 个答案:

答案 0 :(得分:2)

我绕过了这个问题。使用此函数创建名为block.php_code.php的插件文件:

function smarty_block_php_code($params, $content, &$smarty)
{
    if (is_null($content))
    {
        return;
    }
    if ('<?php' == substr($content,0,5) && '?>' == substr($content, -2))
        $content = substr($content,5,-2);
    ob_start();
    eval($content);
    return ob_get_clean();
}

在模板中,您可以写:

{php_code}{literal}<?php

    print "Hello, world!";

?>{/literal}{/php_code}

答案 1 :(得分:2)

它们因某些原因而被折旧,因为它们允许不良做法。 Smarty建议将包含的脚本放入PHP逻辑或创建plugin(这很简单)。

  

{php}标签已从Smarty弃用,不应使用。将PHP逻辑放在PHP脚本或插件函数中。

Source

  来自Smarty的

{include_php}已弃用,请使用已注册的插件将应用程序代码中的演示文稿正确隔离。

Source

如果您在phpfile.php中包含了您要执行的操作,我们可以帮助您编写插件功能。

答案 2 :(得分:0)

相关问题