Smarty PHP функции вставки - PullRequest
0 голосов
/ 02 апреля 2011

Я бы хотел включить подшаблон в основной шаблон.Этот подшаблон должен быть обработан функцией 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) это работает.

Заранее спасибо

1 Ответ

0 голосов
/ 02 апреля 2011

{insert} необходимо вызывать вместе с плагином или функцией, если вы хотите включить другой шаблон, используйте {include}

РЕДАКТИРОВАТЬ: Также обязательно укажите полный путь к включенному вами шаблону, начиная с вашего каталога шаблонов.

...