The example of this article describes the Smarty custom function method, shared for everyone to reference. Specifically as follows:
Purpose of this example: Output times con content (output 4 times Hello World)
Document 1:
Copy Code code as follows:
<?php
Creating Smarty Objects
Require_once ("./libs/smarty.class.php");
$smarty = new Smarty ();
Customizing a function
Description: (1), $arr as an array, (2), TPL call form {test times= "4" size= "5" con= "Hello,world" color= "Red"}
function test ($arr) {
$str = "";
For ($i =0, $i < $arr [' Times ']; $i + +) {
$str. = "<font size= '". $arr [' Size ']. "' color= '. $arr [' Color ']." ' > ". $arr [' con ']." </font> ";
}
return $str;
}
Registration function Registerplugin
$smarty->registerplugin ("function", "Test", "test");//The second parameter is the function name called by the template file, and the third parameter is the custom function name above; corresponding to a corresponding relationship
$smarty->display ("Temp.tpl");
?>
Template file: Temp.tpl
Copy Code code as follows:
Use of {Test times= "3" con= "Hello World" size= "3" color= "green"}
Note: Smarty 3.1.8 has not supported the registration function register_function, should be replaced by Registerplugin
I hope this article will help you with your PHP program design.