The examples in this article describe the use of smarty custom functions. Share to everyone for your reference, specific as follows:
<?php require_once "smarty.config.php";
Customize a function//call method: <{test1 times= "4" size= "5" con= "hello,liuyibao!" color= "Red"}> function Test1 ($args) {$str = ""; For ($i =0, $i < $args [' Times ']; $i + +) {$str. = "<p style= ' font-size:{$args [' Size ']}em;color:{$args [' Color ']} ' >
{$args [' con ']}</p>];
return $str; //Custom Block mode function//Invoke method <{test1}><{/test1}> function Test2 ($args, $con) {$str = ""; for ($i =0; $i < $args ['
Times '] $i + +) {$str. = "<p style= ' font-size:{$args [' Size ']}em;color:{$args [' Color ']} ' >{$con}</p>";}
return $str; //Define a calculation method function Jisuan ($args) {switch ($args [' operate ']) {case "+": $res = $args [' num1 ']-$args [' num2 '];break; case]
-": $res = $args [' num1 ']-$args [' $num 2 '];break;
Case "*": $res = $args [' num1 ']* $args [' $num 2 '];break;
Case "/": $res = $args [' num1 ']/$args [' $num 2 '];break;
return $res;
//Register $smarty->register_function ("Liuyibao", "test1");
The registration block function $smarty->register_block ("Liuyibao2", "test2"); Registration function Jisuan $smarty->register_function ("Jisuan"," Jisuan ");
Substitution variable $smarty->display ("Function.tpl");?>
PS: Here recommend a few of the site's format/landscaping/conversion tools to help you organize the messy code, I believe that we can use in future development:
PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat
JavaScript code Landscaping/compression/formatting/encryption Tools:
http://tools.jb51.net/code/jscompress
Online XML format/compression tools:
Http://tools.jb51.net/code/xmlformat
JSON Code Formatting Landscaping tool:
Http://tools.jb51.net/code/json
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
SQL code Online formatting Landscaping tools:
Http://tools.jb51.net/code/sqlcodeformat
More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on Smarty template.