Smarty User-Defined Function usage example, smarty User-Defined example

Source: Internet
Author: User

Smarty User-Defined Function usage example, smarty User-Defined example

This document describes the usage of smarty udfs. We will share this with you for your reference. The details are as follows:

<? Phprequire_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 ;} // customize a block function // call the function <{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 j. Isuan ($ args) {switch ($ args ['operate']) {case "+ ": $ res = $ args ['num1']-$ args ['num2']; break; case "-": $ res = $ args ['num1']-$ args ['$ num2']; break; case "*": $ res = $ args ['num1'] * $ args ['$ num2']; break; case "/": $ res = $ args ['num1']/$ args ['$ num2']; break;} return $ res ;} // register $ smarty-> register_function ("liuyibao", "test1"); // register the Block Function $ smarty-> register_block ("liuyibao2", "test2 "); // register the jisuan $ smarty-> register_function ("jisu An "," jisuan "); // Replace the variable $ smarty-> display (" function. tpl ");?>

PS: We recommend several formatting, beautification, and conversion tools on this site to help you sort out messy code. I believe you can use them in future development:

Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat

JavaScript code beautification/compression/formatting/encryption tools:
Http://tools.jb51.net/code/jscompress

Online XML formatting/compression tools:
Http://tools.jb51.net/code/xmlformat

JSON code formatting and beautification tools:
Http://tools.jb51.net/code/json

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code online formatting and beautification tools:
Http://tools.jb51.net/code/sqlcodeformat

Related Article

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.