2011-04-02 63 views
0

我想在主模板中包含一個子模板。這個子模板應該由一個php函數來呈現(我需要訪問db)。我在指南中看到{insert}標籤是我應該查找的,因爲include_php已被棄用。現在Smarty PHP插入函數

,我在應該是什麼樣的默認插件目錄(/模板/插件)以下文件:

<?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渲染它),它的工作原理。

在此先感謝

+0

讓它工作? – 2011-04-15 20:18:51

回答

0

{insert}需要的,如果你想要一個插件或功能的結合被稱爲是包含另一個模板使用{include}

編輯:此外,一定要包括完整路徑給你包含從模板目錄開始的模板。