Smarty Custom Function Usage Example _php instance

Source: Internet
Author: User
Tags php template smarty template

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.