【问题标题】:Including PHP file inside smarty template file在 smarty 模板文件中包含 PHP 文件
【发布时间】:2014-07-25 17:53:22
【问题描述】:

我完全不知道 smarty 模板系统。我想要做的是包含一个 php 文件以在 .tpl 文件中获取一些变量(这是一个 WHMCS 模板)。

我试过这样:

{php} include ('file.php'); {/php} //doesn't work

{include_php file='file.php'}  //doesn't work

我也关注了this question的回答。仍然无法正常工作。

如何在 WHMCS 的 header.tpl 中包含 code.php?有什么帮助吗?

仅供参考:tpl 和 php 文件都在同一个目录中,如果有帮助的话。

【问题讨论】:

    标签: php include smarty whmcs


    【解决方案1】:

    Smarty 中确实不推荐使用 php 代码。事实上它现在已经被弃用了,你应该尽可能避免这样的解决方案,因为它通常没有意义。

    但是,如果您出于某种原因确实想在 Smarty 文件中使用 PHP,则需要使用 SmartyBC(Smarty 向后兼容性)类而不是 Smarty 类。 p>

    例如,而不是:

    require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
    $smarty = new Smarty();
    

    你应该使用:

    require_once('SmartyBC.class.php');
    $smarty = new SmartyBC();
    

    然后您就可以在 Smarty 模板文件中使用 PHP

    编辑

    如果您只是在包含问题时遇到问题,则可能是您的目录问题(但是您没有显示任何错误)。

    我假设您的模板目录中有您的文件,并且您使用以下方法正确设置它:

    $smarty->setTemplateDir('templates');
    

    如果您在 Smarty 中简单地显示 index.tpl 文件,并且此 PHP 文件位于同一目录中(在 template 目录中),您可以在没有路径的情况下包含它。

    但是如果你在这个index.tpl文件中包含另一个tpl文件,当你想要包含php文件时你需要传递这个PHP文件的完整路径,例如:

    {include_php 'templates/file.php''}
    

    【讨论】:

    • 嗯,在 whmcs 模板中,{php} 确实有效。我面临的唯一问题是php include
    • @тнєSufi 那么 Smart 会显示什么错误?你的目录结构是什么?您如何使用 Smarty?如果没有更多代码和细节,我似乎无法为您提供帮助
    • 正如我之前在我的问题中所说的,我不熟悉 smarty 系统,这是 WHMCS 模板。结构类似于whmcsdirectory/templates/templatename/[tpl-files]。我认为他们使用 v2 的 smarty。而且我不知道如何在 smarty 上启用错误检查。我要搜索一下,并会相应地更新。
    • 您可以使用$smarty->error_reporting = E_ALL 设置error_reporting。您可能应该尝试所有可能性:whmcsdirectory/templates/templatename/phpfile.phptemplates/templatename/phpfile.phptemplatename/phpfile.phpphpfile.php - 其中一种应该更有效。您可能还将 $smarty->security 设置为 on,然后您只能包含来自 trusted_dir -> smarty.net/docsv2/en/variable.trusted.dir.tpl 的 PHP 文件
    【解决方案2】:

    您以错误的方式使用 Smarty。 Smarty 的重点是不在您的演示文稿位中包含任何 PHP(视图,也就是好的 ol' HTML)。

    所以,无论你想在那个 PHP 文件中做什么,只要让它发挥它的魔力,但将实际结果发送给 Smarty。例如,您想显示从数据库中获取的用户表吗?执行查询,获取结果并将结果(如结果数组)传递给 smarty,如下所示:

    <?php
    $smarty = new Smarty();
    $users = $db->query('SELECT * FROM users');
    
    // Assign query results to template file.
    $smarty->assign('users', $users);
    
    // Compile and display the template.
    $smarty->display('header.tpl');
    

    现在,在您的 smarty 模板中,您可以像这样访问该数组:

    <html>
        {foreach from=$users item=user}
            Username: {$user->username}<br />
        {/foreach}
    </html>
    

    我希望你知道我要去哪里。将您的应用程序逻辑保存在 PHP 文件中,让模板处理外观。使模板尽可能地愚蠢!

    【讨论】:

    • 感谢您的详细解释。我用你的方法试过了,但没有用。我的 php 和 tpl 文件都在同一个目录中。而且它们的名字不同。那是问题吗?还是我错过了什么?
    • 不,您不必使用相同的名称。只需确保您也执行$smarty-&gt;display('yourtemplate.tpl'); 来实际编译和显示您的模板。你要在那个 PHP 文件中做什么?
    • 在那个 PHP 文件中,我得到了几个数据库结果,一些在数组中,一些在字符串中,我需要在 3 个不同的 tpl 文件中显示。我会尝试使用$smarty-&gt;display 并更新。
    【解决方案3】:

    您通过分配数据将数据输入 Smarty。不推荐嵌入 PHP,Smarty 3 已弃用。

    php:

    $smarty->assign('foo','bar');
    

    聪明:

    {$foo}
    

    【讨论】:

    • 这是 smarty 的旧版本,{php} 在这里工作。我面临的唯一问题是php include
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多