This article mainly introduces the methods for customizing functions in the smarty template engine. The example analyzes the definition, registration, and calling skills of custom functions, for more information about the smarty UDF method, see the following example. The details are as follows:
Purpose: To output times con content (output 4 times hello world)
File 1:
The Code is as follows:
<? Php
// Create a smarty object
Require_once ("./libs/Smarty. class. php ");
$ Smarty = new Smarty ();
// Customize a function
// Description: (1), $ arr is 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. = "". $ arr ['Con ']. "";
}
Return $ str;
}
// Register the registerPlugin Function
$ Smarty-> registerPlugin ("function", "test", "test"); // The second parameter is the name of the function called by the template file, variable; the third parameter is the name of the previously defined function.
$ Smarty-> display ("temp. tpl ");
?>
Template File: temp. tpl
The Code is as follows:
Use of smarty custom functions
{Test times = "3" con = "hello world" size = "3" color = "green "}
Note: The registry function register_function is not supported in smarty 3.1.8. Replace it with registerPlugin.
I hope this article will help you with php programming.