How to customize functions in smarty template engine, smarty template engine function
The example in this article describes the smarty custom function method and is shared with everyone for your reference. The details are as follows:
The purpose of this example: output the content of times con (output hello world 4 times)
File 1:
Copy code The code is as follows:
//Create smarty object
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
//Customize a function
//Explanation: (1), $arr is an array; (2), tpl calling form {test times="4" size="5" con="hello,world" color="red"}
function test($arr){
$str = "";
for($i=0;$i<$arr['times'];$i++){
$str .= "
".$arr['con']. "";
}
return $str;
}
//Register function registerPlugin
$smarty->registerPlugin("function","test","test");//The second parameter is the function name called by the template file, which is variable; the third parameter is the custom function name above; corresponding to a corresponding relationship
$smarty->display("temp.tpl");
?>
Template file: temp.tpl
Copy code The code is as follows:
Usage of smarty custom functions
{test times="3" con="hello world" size="3" color="green"}
Note: smarty 3.1.8 no longer supports the registration function register_function and should be replaced by registerPlugin
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/946748.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946748.htmlTechArticleHow to customize functions in smarty template engine, smarty template engine function This article describes the smarty custom function method in an example, Share it with everyone for your reference. The details are as follows: This example...