繁体   English   中英

Smarty PHP插入函数

[英]Smarty PHP insert functions

我想在主模板中包含一个子模板。 此子模板应由php函数呈现(我需要访问数据库)。 在指南中,我已经看到{insert}标签是我应该寻找的,因为include_php已被弃用。

现在,我在默认插件目录(/ templates / plugins)中有以下文件:

<?php

    // /templates/plugins/insert.admin_items.php

    require_once('lib/smarty/Smarty.class.php');

    function smarty_insert_admin_items($params, &$smarty)
    {
        /* fetch items */

       // render page
       $smarty = new Smarty();
       $smarty->assign('items', $sorted_items);
       return $smarty->fetch('admin_items.tpl');    
    }
 ?>

包含的子模板:

<!-- /templates/admin_items.tpl -->
<div>
  {foreach $items as $i}
    <div>{$i.title}</div>
  {/foreach}
</div>

这是主模板

<!-- /templates/admin.tpl -->
<html>
    <body>
       {insert name="admin_items"}
   </body>
</html>

以及我怎么称呼它

// /admin.php
<?php
    require_once('lib/smarty/Smarty.class.php');

    $smarty = new Smarty();
    $smarty->display('admin.tpl');
?>

这给了我以下错误:

Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "./templates/admin.tpl" on line 10 "{insert name=&quot;admin_items&quot;}" {insert} no function or plugin found for 'admin_items'' in /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_templatecompilerbase.php:431 Stack trace: #0 /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_compile_insert.php(92): Smarty_Internal_TemplateCompilerBase->trigger_template_error('{insert} no fun...', 10) #1 /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_templatecompilerbase.php(284): Smarty_Internal_Compile_Insert->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL) #2 /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_templatecompilerbase.php(123): Smarty_Internal_TemplateCompilerBase->callTagCompiler('insert', Array, Array) #3 /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_templateparser.php(2319): Smarty_Internal_TemplateC in /Applications/MAMP/htdocs/bo/lib/smarty/sysplugins/smarty_internal_templatecompilerbase.php on line 431

请注意,无需使用子模板/插入(从admin.php获取数据并将其呈现在admin.tpl中),它就可以工作。

提前致谢

如果您要包含其他模板,则需要结合插件或函数来调用{insert }

编辑:还请确保从模板目录开始包含您所包含模板的完整路径。

暂无
暂无

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

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