Custom functions for Smarty templates (common in this article)
Divided into three categories:
1. Variable Adjuster
2. Functions
3. Block function
Usage of three kinds of plugins:
1, the use of variable mediators,
< {$var|myfun:arg1:arg2} >
2. Function usage (much like using HTML tags)
Such as
<colorsizenum= "7"}>
The functions defined in PHP are:
$smarty->registerplugin ("function", "Myfun","one"); function One ($args,$smarty) {} //$args color= "Red" size= "7" num= "7" These parameters are combined with an associative array, and the second parameter is automatically received by the $smarty object, if not written
3, the use of block functions,
Such as
<colorsize= "7"}> content <{/myfun }>
The functions defined in PHP are:
$smarty->registerplugin ("block", "Myfun","I")Function ($args ,$content,$smarty) {} //$args An associative array that combines these parameters for color= "Red" size= "7", The second parameter is the "content" to be displayed, and the third parameter is the $smarty object
There are two ways of doing this:
1. Use the Registerplugin () method in the Smarty object to register functions in PHP as functions in Smarty.
2, is the development of the Smarty plug-in (to separate specific files to add plug-ins)
1) Location of the Declaration
Can be created in the original plugins directory in the Smarty class library, or you can use the directory $smarty->addpluginsdir (directory) you specified//smarty3
2) Name of the file
modifier, modifier. Modifier name. php
Functions, functions. Name of the function. php
Block function, block function name. php
3) Naming of functions
modifier, Smarty_modifier_ modifier name ()
function, Smarty_function_ function name ()
Block function, Smarty_block_ block function name ()
4) Rules for parameters
modifier, smarty_modifier_ modifier name ($var, $arg 1, $arg 2, $arg 3)
function, Smarty_function_ function name ($args, $smarty)
Block function, Smarty_block_ block function ($args, $content, $smarty, $repeat)
$repeat parameter prevents the block function from being called repeatedly, and the first call is true and later false
<{myfun color= "Red" size= "7"}> content <{/myfun}> First Myfun true, second <{/myfun}> false
Iv. custom functions for Smarty templates