如何在Smarty上创建插件?

问题描述

    {PHP}
    $ssa = array(3,4,5,6,7,8);
    $sl = array();

    $this->assign('shoe_sizes',$ssa);
    $this->assign('sl',$sl);

    {/PHP}

我在smarty模板中有此PHP代码,但smarty不允许/建议在具有smarty3升级的模板中使用{PHP}标签。该脚本位于index.tpl.html中,这是我在index.tpl.html中使用它的位置:

{foreach from = $shoe_sizes item=shoe}

我想将PHP代码与tpl文件分开。我的测试环境中没有任何插件文件夹或路径。我在Google上进行了搜索,但感到困惑。

我应该创建一个名为plugin的新文件夹并在其中编写脚本吗?

这是我最近尝试过的: 在网站/products/index.tpl.html文件中;

{include file="plugins/function.shoe-size.PHP"}
  {foreach from = $shoe_sizes item=shoe}
    {if $info.return eq $shoe and $info.return|substr:0:1 != "0"}
      <p style="font-weight:bold;display:inline;border-bottom:solid 1px black;">SHOE: </p>
    {/if}
   {/foreach}

在网站/插件/function.shoe-size.PHP

<?PHP

/**
 * Smarty plugin
 */

/**
 * Smarty {url} function plugin
 *
 * Type:     function
 * Name:     Shoe Size filter 
 */

function smarty_function_shoeSizes() {
    $shoe_sizes = array(3,8);
    return $shoe_sizes;
}

?>

这是错误

无法在以下位置加载模板'file:plugins / function.shoe-size.PHP' 'products / index.tpl.html'

谢谢

解决方法

您不能在模板中包含php文件。 将您的smarty插件移动到插件目录(默认为smarty / smarty / libs / plugins /) 或创建并设置自定义插件目录(https://www.smarty.net/docs/en/api.set.plugins.dir.tpl) 那么,您不必每次都在模板中包含插件-只需使用(https://www.smarty.net/docs/en/language.function.function.tpl