June 5 Smarty Custom function

Source: Internet
Author: User

Custom Function: <{method name}>

In the HTML page can be directly assigned to: (no function just know can)

<{$a = "Hello"}>
<div><{$a}></div>

Display effect: Hello

1.<{literal}><{/literal}> built-in functions

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. If you need to use a template tag in the {literal} block, consider escaping the separate delimiter with {Ldelim}{rdelim}.

The function is when the left and right delimiter {} In case of mixing with function, but it is not used now <{}>

For example

 <{literal}> <{foreach   $shuju  as   $v }><tr> <td> <{ $v  [1]|truncate:10: "* * *" |fontcolor:green}></td> <td> <{ $v  [2]|fontcolor}></td> <td><{ $v  [3]|kmname}></td> <td><{ $v  [4]|nandu}></ td> <td><{ $v  [5]|type}></td> <td><a href= "Shanc Hu.php?code=<{ $v  [0]}> "> Delete </a>&nbsp;&nbsp;<a href = "Xiugai.php?code=<{ $v  [0]}>" > Modify </a></td> </tr> <{/foreach }><{/literal}> 

The result of the output at this time is: (not parsed as output)

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.

For example 1:<div><{html_select_date}></div> (in case of no write-in parameters)

The returned result is:

For example 2:<div><{html_select_date start_year= ' 1990 ' end_year= ' 2020 ' month_format= '%m '}></div> (write-in parameter)

The returned result is:

3. Online Search: Color controlCreate a new function.color.php (function) in Www/smarty0603/plugins, and also introduce the Jscolor.js file
<?PHP/** * Color Control * * @param $name Control name * @param $value Check value * <{color name= ' titlecolor ' value= ' ff00ff '}>*/    functionSmarty_function_color ($args,$smarty) {        $name=$args[' Name ']; $value= !Empty($args[' Value ']) ?$args[' Value ']: ' 000000 '; 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. ' "/> '; return $str; }
View Code

Previous HTML code: <div><{color name= ' AA '}></div>

return Result:

4.{block} BLOCK: Editor (online search){Block}{/block} paired appearance

Common method functions only need to write a good, but think block function has beginning and end

New block.textarea.php
<?PHP/** * editor * @param int $textareaid * @param int $toolbar have basic full and desc three types * @param int $color Editor Color * @param string $alowuploadexts allow upload type * @param string $height Editor Height * @param string $disabled _page whether paging is disabled and Sub-headings*/    functionSmarty_block_textarea ($args,$content,$smarty, &$repeat) {    //Public static function editor ($textareaid = ' content ', $toolbar = ' basic ', $height = $, $color = ', $up =true) {    if(!$repeat) {            $textareaid= !Empty($args[' Name ']) ?$args[' Name ']: ' content '; $toolbar= !Empty($args[' toolbar ']) ?$args[' toolbar ']: ' Basic '; $height= !Empty($args[' Height ']) ?$args[' Height ']: ' 200 '; $color= !Empty($args[' Color ']) ?$args[' Color ']: '; $up= !Empty($args[' Up ']) ?$args[' Up ']:true; $str= ' <textarea name= '.$textareaid.‘" > '.$content.‘ </textarea> '; if(!defined(' Editor_init ')) {            $str. = ' <script type= ' text/javascript ' src= '. /js/ckeditor/ckeditor.js "></script>"; Define(' Editor_init ', 1); }            if($toolbar= = ' Basic ') {            $toolbar= "[' Bold ', ' Italic ', ' underline ', ' Strike ', ' numberedlist ', ' BulletedList ', ' textcolor ', ' BGColor ', ' Link ', ' Unlink ', '-', ' Image ', ' Flash ', ' Table ', ' Smiley ', ' Specialchar '],[' removeformat '], \ r \ n"; } ElseIf($toolbar= = ' Full ') {           $toolbar= "[' Source ', '-', ' Templates '], [' Cut ', ' Copy ', ' Paste ', ' pastetext ', ' Pastefromword ', '-', ' Print '], [' Un Do ', ' Redo ', '-', ' Find ', ' Replace ', '-', ' selectall ', ' Removeformat '],[' showblocks '],[' Image ', ' Capture ', ' Flash '],[' Maximize '], '/', [' Bold ', ' Italic ', ' underline ', ' Strike ', '-'], [' Subscript ', ' superscript ', ' -'], [' numberedlist ', ' BulletedList ', '-', ' outdent ', ' Indent ', ' Blockquote '], [' Justifyleft ', ' Justifycen ' Ter ', ' justifyright ', ' Justifyblock '], [' Link ', ' Unlink ', ' Anchor '], [' Table ', ' horizontalrule ', ' Smiley ',            ' Specialchar ', '/', [' Styles ', ' Format ', ' Font ', ' FontSize '], [' TextColor ', ' BGColor '], [' Attachment '],\r\n"; } ElseIf($toolbar= = ' desc ') {            $toolbar= ' [' Bold ', ' Italic ', '-', ' numberedlist ', ' BulletedList ', '-', ' Link ', ' Unlink ', '-', ' Image ', '-', ' Source '],\r\n '; } Else {            $toolbar= ' '; }        $str. = "<script type=\" text/javascript\ ">\r\n"; $str. = "Ckeditor.replace ('$textareaid‘,{"; $str. = "height:{$height},"; if($color) {            $str. = "Extraplugins: ' Uicolor ', Uicolor: '$color‘,"; }        /*if ($up) {$str. = "Filebrowserimageuploadurl: '". B_url. "            /upimage ', "; $str. = "Filebrowserflashuploadurl: '". B_url. "        /upflash ', "; } */        $str. = "toolbar: \ r \ n"; $str. = "[\ r \ n"; $str.=$toolbar; $str. = "]\r\n"; //$str. = "Fullpage:true";        $str. = "}); \ r \ n"; $str. = ' </script> '; return $str; }    }
View Code

Previous HTML code:<{textarea}><{/textarea}> when no parameters are added

Show effect

Previous HTML code: <{textarea name= ' text ' toolbar= ' full ' color= ' #f0f ' height= '}><{/textarea}> ' plus parameter

Show effect

5. Practice writing the function yourself (before the regulator is now based on the method)

New File function.tyname.php

<?PHPfunctionSmarty_function_tyname ($args){    //fetch the code value passed in.    $code=$args["Code"]; $db=NewMysqli ("localhost", "root", "" "," MyDB "); $sql= "Select type from leixing where code = ' {$code}‘"; $result=$db->query ($sql); $attr=$result-Fetch_row (); return $attr[0];}
View Code

HTML code for the previous paragraph: <td><{tyname code= $v [5]}></td>

Display effect: Previous effect:

6. Practice Custom Functions Block: Define block functions to change the style of deleting changesnew File block.fontstyle.php (four parameters required)
<?PHPfunctionSmarty_block_fontstyle ($args,$content,$smarty,$a){    //$args: Is the parameter that the user can pass in, $content: the content contained within the block function; $a: Refers to the beginning and end of a function    $color=$args["Color"]; $size=$args["Size"]; //The block function executes 2 times, each time the start and end tags are executed//To prevent repeated execution, which needs to be judged    if(!$a)//If the end tag is executed    {         return"<span style= ' background-color:{$color};font-size:{$size}px ' >{$content}</span> "; }}

Front-end HTML code:

  <TD>    <{FontStyleColor= ' Yellow 'size= ' + '}>    <ahref= "shanchu.php?code=<{$v [0]}>">Delete</a>&nbsp;&nbsp;<ahref= "xiugai.php?code=<{$v [0]}>">Modify</a>    <{/fontstyle}>    </TD> 

Display effect:

7. Configuration file: Config filesCreate a new file in the Configs folder, with the file name ending 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 in the front main.html, change its style with a configuration file: The new style.conf in the configs configuration file looks like this:

Front-end HTML code:

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>

Display effect:

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>

The effect is as follows:

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

The effect is as follows:

June 5 Smarty Custom 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.