How to create plugin on Smarty?

1k Views Asked by At
    {php}
    $ssa = array(3,4,5,6,7,8);
    $sl = array();

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

    {/php}

I have this php code in smarty template but smarty doesn't allow/recommend {php} tag using in templates with smarty3 upgrade. This script is in index.tpl.html and this is where i am using it in index.tpl.html :

{foreach from = $shoe_sizes item=shoe}

I would like to separate php code from tpl file. I don't have any plugin folder or path in my test env.I have searched on google but getting confused.

Should i create a new folder which is called plugin and to write my script there ?

This is what i tried recently: in web-site/products/index.tpl.html file;

{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}

in web-site/plugins/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,4,5,6,7,8);
    return $shoe_sizes;
}

?>

This is the error:

Unable to load template 'file:plugins/function.shoe-size.php' in 'products/index.tpl.html'

Thanks

1

There are 1 best solutions below

0
On

You cannot include php files in templates. Move your smarty plugin to plugin directory (default is smarty/smarty/libs/plugins/) or create and set custom plugin directory (https://www.smarty.net/docs/en/api.set.plugins.dir.tpl) then, you don't have to include plugin every time in template - just use it (https://www.smarty.net/docs/en/language.function.function.tpl)