我想在主模板中包含一个子模板。这个子模板应该由php函数呈现(我需要访问db)。我在指南中看到{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="admin_items"}" {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
请注意,如果不使用subtemplate / insert事件(从admin.php获取数据并在admin.tpl中呈现它),它就可以工作。
提前致谢