简体   繁体   English

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

[英]Smarty: how to include php in template?

I've tried {include_php file="phpfile.php"} and {php} tags but both cause deprecated error.我试过{include_php file="phpfile.php"}{php}标记,但都导致不推荐使用的错误。 Can you not do this in Smarty anymore?你不能再在 Smarty 中这样做吗? I can't find anything in the docs.我在文档中找不到任何内容。

I circumvented this problem.我绕过了这个问题。 Create a plugin file named block.php_code.php with this function in it:创建一个名为block.php_code.php的插件文件,其中包含此 function:

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();
}

In your template, you can then write:在您的模板中,您可以编写:

{php_code}{literal}<?php

    print "Hello, world!";

?>{/literal}{/php_code}

They are depreciated for a reason as they allow poor practices.它们被贬值是有原因的,因为它们允许不良做法。 Smarty recommends putting the included script into the PHP logic or creating a plugin (which is simple). Smarty 建议将包含的脚本放入 PHP 逻辑或创建一个插件(这很简单)。

{php} tags are deprecated from Smarty, and should not be used. {php} 标签已从 Smarty 中弃用,不应使用。 Put your PHP logic in PHP scripts or plugin functions instead.将您的 PHP 逻辑放在 PHP 脚本或插件函数中。

Source资源

{include_php} is deprecated from Smarty, use registered plugins to properly insulate presentation from the application code. {include_php} 已从 Smarty 中弃用,请使用已注册的插件将演示文稿与应用程序代码正确隔离。

Source资源

If you include what you are trying to do in your phpfile.php , we can help you write a plugin function.如果您在phpfile.php中包含您想要做的事情,我们可以帮助您编写插件 function。

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

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