Plug - ins
is loaded on demand. Plugins are loaded when specific modifiers, functions, resources, and so on are called.
Plugin directory variables
Can be a string of a path or an array that contains multiple paths.
How to install plug-ins
is to put the plug-in files directly in these directories, Smarty will be automatically used as needed.
Set the plugin directory (create a new Myplugins folder)
D:\wamp\www\test\smarty\libs\plugins\
D:\wamp\www\test\smarty\libs\myplugins\
Define the root directory define (' root ', str_replace ("\ \", "/", DirName (__file__)). " /");///Note Add a plug-in to the system by default, you will not be able to use the default system plug-in $smarty->setpluginsdir (Array (ROOT). /libs/plugins/",//system default set path ROOT." /libs/myplugins/",//Custom));
2. defining Plug-in functions
<?php/* * 1. Determine the plug-in location $smarty->setplugsdir (); * 2. The file name must have a rule, in order to determine the file name by the filename modifier. php * 3. The function name must have a rule in order to find this function in this file * 4. Parameters must have a rule the first one has to be processed. The second is the parameter to use */FU Nction Smarty_modifier_mystyle ($str, $size = "3", $color = "Pink") {return ' <font color= ' {$color} ' siz E= ' {$size} ' >{$str}</font> "; }
3. Template Call plugin (decorator)
<{$sex |mystyle:6:red}>
4. Browser Output
Male x
Attach PHP page code
<?php//Create Smarty Object require_once './libs/smarty.class.php ';//define root directory define (' root ', str_replace ("\ \", "/", DirName (__ file__)). " /");//Instantiate Smarty Class $smarty=new smarty ();//Set delimiter $smarty->left_delimiter=" <{"; $smarty->right_delimiter="} > ";//set to false the bounding symbol can have spaces around $smarty->auto_literal = false;//Add a plugin to the directory//$smarty->setpluginsdir (ROOT.") /libs/myplugins/");//pay attention to adding a plug-in, to add the system default path to add the default system can not use the plug-in $smarty->setpluginsdir (" ROOT. " /libs/plugins/",//system default set path ROOT." /libs/myplugins/",//Custom));//Get the directory of the plugin var_dump ($smarty->getpluginsdir ());//variable Assignment $smarty->assign (' sex ', ' male X ');//variable Output $smarty->display (' Hello.tpl ');? >
This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1843672
PHP Smarty Template Custom Plugin