How to use custom functions in the smarty Template

Source: Internet
Author: User

When using the PHP smarty template engine, you sometimes need to call some developer-defined functions, such as an encode function. This function is not embedded in smarty, so naturally, you need to first construct this function in PHP code, and then call this function in the template folder. The method is as follows:

 

For example, a function is designed in Foo. php.

 

 

 

/** <Br/> * the foo. php file generates a sub function, which is called through the smarty template. <Br/> */<br/> <? Php <br/> $ S = new smarty (); <br/> Function Sub ($ A, $ B) {<br/> return $ A + $ B; <br/>}< br/> $ S-> assign ("foo", "foo.html"); <br/>?> 

 

The corresponding page can be displayed in the foo.html file after the authorization value is passed.

 

<! -- The above PHP file uses $ S-> display () to generate corresponding variables for the following HTML file: <br/> <HTML> <br/>... <br/> <{if $ browsertype = "ie"}> <br/> <TD align = "center"> <br/> <a target = "_ blank" href = "http://pharma.dxy.cn/info/productsearch.htm? Keyword = <{$ result [A]. drugname | keywordsencode: $ result [A]. drugname }>< br/> & field = 1 & Order = 0 & limit = 10 & infotype = 6 & category = & releasedate = 0 & agencyname = & Area = & code ="> <{$ result [A]. drugname }></A> <br/> </TD> <br/> <{else}> <br/> <TD align = "center"> <br/> <a target = "_ blank" href = "http://pharma.dxy.cn/info/productsearch.htm? Keyword = <{$ result [A]. drugname | substring: $ result [A]. drugname }>< br/> & field = 1 & Order = 0 & limit = 10 & infotype = 6 & category = & releasedate = 0 & agencyname = & Area = & code ="> <{$ result [A]. drugname }></A> <br/> </TD> <br/> <{/if}> <br/> </ptml> 

 

The above code

<{$ Result [A]. drugname | keywordsencode: $ result [A]. drugname}> indicates:

 

 

Use | to call the keywordsencode method in the PHP file. Use: To represent the parameter of the Parameter Function.

 

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.