Smarty Custom Functions

Source: Internet
Author: User

Custom Function: <{method name}> creates a new file in Lib/plugins, named in a fixed way: function. Method name. php or block. Method name. php

1.<{literal}><{/literal}>

The data in the {literal} label area will be processed literally, typically around the javascript/css block, or the curly braces ' {', '} ' used in these languages will interfere with the template delimiter syntax. {literal} All symbols inside the {/literal} tag are not interpreted and are output as-is.

For example: After adding tags to the main.html code:

Operation Result:

2.{html_select_date}

The custom function {html_select_date} is used to create a date drop-down list, which can display any month or day. Parameters not described in the following list are displayed as key value pairs of name/value in the corresponding year, month, Day <select> label.

<div><{html_select_date}></div>

Display interface:

After adding parameters:

<div><{html_select_date start_year= ' 1990 ' end_year= ' 2020 ' month_format= '%m '}></div>

Display interface:

3. Online Search: Color control function.color.php (need to introduce file jscolor.js)
1<?PHP2     3     /**4 * Color Control5      * 6 * @param $name control name7 * @param $value selected value8 * <{color name= ' titlecolor ' value= ' ff00ff '}>9      */Ten     functionSmarty_function_color ($args,$smarty) { One  A         $name=$args[' Name ']; -         $value= !Empty($args[' Value ']) ?$args[' Value ']: ' 000000 '; -      the         if(!defined(' Color_init ')) { -             Define(' Color_init ', 1); -             $str= ' <script src= '. /js/jscolor/jscolor.js "></script>"; -                 +         } -         $str. = ' <input class= ' "Color" style= "Width:48px;height:16px;overfrom:hidden" name= "'.$name. ' "Value=".$value. ' "/> '; +      A         return $str; at}
View CodeCalling method on the front end: <div><{color name= ' AA '}></div>

After operation: can be arbitrarily selected

4.{block} BLOCK: Editor (on-line) {Block}{/block} appears in pairs for example: Introduction: block.textarea.php (also CKEditor folder) in the front-end call Method://No parameters:
<{textarea}>
<{/textarea}> after Operation:

Add parameter:
<{textarea name= ' text ' toolbar= ' full ' color= ' Purple ' height= ' '}> '
<{/textarea}> after Operation:

5. Practice the Custom Function: Write method to change the main.html type to show the specific content new file function.typename.php
1<?PHP2 3 //parameters: 1. $args array, must have4 functionSmarty_function_typename ($args)5 {6     //fetch the code value passed in.7     $code=$args["Code"];8     if($code==0)9     {Ten         return"Judgment question"; One         } A     Else if($code==1) -     { -         return"Single Choice"; the         } -     Else -     { -         return"Multi-choice"; +         } -}
After the front-end call method: Main.html <td><{typename code= $v [5]}></td> run:

6. Practice Custom Functions Block: Define block functions to change the style of deleting modified new file function.typename.php (four parameters required)
1<?PHP2 //$args is the parameter that the user can pass in $a the function is the start and end3 functionSmarty_block_fontstyle ($args,$content,$smarty,$a)4 {5     $color=$args["Color"];6     $size=$args["Size"];7     8     //The block function executes 2 times and executes once at the start and end tags9 //To prevent repeated execution, need to be judgedTen     if(!$a)//If the end tag is executed One     { A         return"<span style= ' background-color:{$color};font-size:{$size}px ' >{$content}</span> "; -          -     } the}

Calling methods on the front end: main.html
1   <!--define block functions to change the style of deleting modifications-2      <td>3      <{fontstyle color= ' yellow ' size= '}> 4     <a href= "delete.php?code=<{$v[0]}>" > Delete </a>5     <a href= "update.php? code=<{$v[0]}> "> Modify </a>6     <{/fontstyle}>7     </ Td>

After running: (the middle Operation accidentally deleted a row of data)

7. configuration file: Config files creates a new file in the Configs folder, and the file name ends with. conf For example: The test.conf configuration file is loaded into the template via the built-in function {config_load} or the Configload () function of the API. Example: Write a div on the front main.html and change its style with the configuration file:

<div> below is a list of topics </div>

Create a new file in the Configs folder style.conf

On the front main.html:

1. Read the configuration file: <{config_load file= "style.conf"}>

2. Introducing variables with <{#变量名 #}>: <div style= "font-size:<{#size #}>px; color:<{#color #}> "> Below is a list of topics </div>

If: The same style is defined in style.conf, it is distinguished by a block, defined in the following way: [Block name]

In front of main.html: Give Config_load the second variable section

<{config_load file= "style.conf" section= "one"}>
<div style= "font-size:<{#size #}>px; color:<{#color #}> "> Below is a list of topics </div>

<{config_load file= "style.conf" section= "Both"}>
<div style= "font-size:<{#size #}>px; color:<{#color #}> "> Below is a list of topics </div>

Smarty Custom Functions

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.