Smarty Variable Adjuster

Source: Internet
Author: User

Variable regulator: <{$a | variable Regulator}>

Find out more Smarty Manuals

Mainly modify the information on this page to understand the variable regulator: main.php/main.html (0603)

1. Using a given variable regulator

Capitalize first Letter Capital

PHP page: $smarty->assign ("Ceshi", "Hello World");

HTML page: <div><{$ceshi |capitalize}></div>

After run:Hello World

The truncate intercepts the string (with parameters), and 10 represents the length of the Intercept. is the third parameter that represents the remaining content

<td><{$v [1]|truncate:10: "..."}></td> can also be used:<td><{$v [1]|truncate:10}></td > (default is ...)

After Operation: Original:

2. Self-written variable adjuster To create a new file in Lib/plugins, the naming method is fixed: modifier. variable adjuster name. PHP 2.1 Changing the color of the answerNew file: modifoer.fontcolor.php
1 <? PHP 2 // parameters: The first is the default string $str, which must be written 3 function smarty_modifier_fontcolor ($str,$color= "green")4{  5     return "<span style= ' color:{$color} ' >$str</ Span> "; 6     }
calling methods on the front main.html: <td><{$v [2]|fontcolor:red}></td>

2.2 Combination using variable Adjuster: Call method main.html in front end: <td><{$v [1]|truncate:10: "..." |fontcolor}></td> 2.3 Display the name of the owning account new file: modifoer.kmname.php
1<?PHP2 functionSmarty_modifier_kmname ($str)3 {4     $conn=NewMysqli ("localhost", "root", "" "," MyDB "); 5     $sql= "SELECT name from Kemu where code = ' {$str}‘";6     $result=$conn->query ($sql);7     $attr=$result-Fetch_row ();8     return $attr[0];9}
Calling methods on the front main.html: <td><{$v [3]|kmname}></td>

2.4 Display the name of difficulty new file: modifoer.nandu.php
1<?PHP2 functionSmarty_modifier_nandu ($str)3 {4     if($str==0)5     {6         returnSimple;7         }8     Else if($str==1)9     {Ten         returnModerate; One         } A     Else -     { -         returnDifficult; the         } -}
Calling methods on the front main.html: <td><{$v [4]|nandu}></td>

2.5 Name of the display type new file: modifoer.type.php
1<?PHP2 functionSmarty_modifier_type ($str)3 {4     if($str==0)5     {6         returnJudgment;7         }8     Else if($str==1)9     {Ten         return"Single-Choice"; One         } A     Else -     { -         return"Multi-choice"; the         } -}

Calling methods on the front main.html: <td><{$v [5]|type}></td>

Main.html all changes in the content:

<{foreach $shuju as $v}>
<tr>
<!--truncate intercepts the string (with parameters), and 10 represents the length of the Intercept. is the third parameter that represents the remaining content- -
<td><{$v [1]|truncate:10: "..." |fontcolor}></td>
<td><{$v [2]|fontcolor:red}></td>
<td><{$v [3]|kmname}></td>
<td><{$v [4]|nandu}></td>
<td><{$v [5]|type}></td>
<td><a href= "delete.php?code=<{$v [0]}>" > Delete </a>
<a href= "update.php?code=<{$v [0]}>" > Modify </a>
</td>
</tr>
<{/foreach}>

1<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title> Home </title>67 8<body>9Ten<table width= "70%" border= "1" cellpadding= "0" cellspacing= "0" > One<tr> A<td> Topic name </td> -<td> Answers </td> -<td> Subjects </td> the<td> Difficulty </td> -<td> type </td> -<td> Operations </td> -</tr> +  -<{foreach $shuju  as $v}> +<tr> A<!--truncate intercepts the string (with parameters), and 10 represents the length of the Intercept. is the third parameter that represents the remaining content-- at<td><{$v[1]|truncate:10: "..." |fontcolor}></td> -<td><{$v[2]|fontcolor:red}></td> -<td><{$v[3]|kmname}></td> -<td><{$v[4]|nandu}></td> -<td><{$v[5]|type}></td> -<td><a href= "delete.php?code=<{$v[0]} > > Delete </a> in<a href= "update.php?code=<{$v[0]} > > Modify </a> -</td> to</tr> +<{/foreach}> -</table><br/> the  *<a href= "add.php" > Add Data </a><br/> $ Panax Notoginseng</body> -View Code

Smarty Variable Adjuster

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.