PHP template engine smarty variable regulator and custom variable operators

Source: Internet
Author: User

Variable RegulatorUsed for variables, user-defined functions, and strings. Use the '|' symbol and the regulator name to apply the regulator. The behavior of a variable regulator is determined by the value of the parameter. Parameters are separated by the ':' symbol.

The custom functions in the smarty template engine are stored in the plug-in directory (plugins). The built-in functions are written in the syntax of smarty. User-defined functions can be modified at will, and can be added at will. built-in functions are not.

We can customize the function file in the Plugins directory, but we must strictly follow the naming rules of the smarty function, so that when we call the custom function, smarty can be found under the Plugins.

Next, we will define two user-defined functions:

    • The stredit function is used to convert the case sensitivity of strings.
    • The jiequ function is used to intercept strings.

Naming rules:

    • The variable regulator file name must start with modifier, followed by a custom function name, such as modifier. stredit. php, modifier. jiequ. php
    • The name of the variable regulator function must start with smarty_modifier _ and be connected to the custom function name, for example, smarty_modifier_stredit () and smarty_modifier_jiequ ()
Custom string operation functions

<? PHP
/*
* Smarty plugin
* Custom string case-sensitivity Conversion Function
*/
Function Smarty_modifier_stredit ( $ Str , $ Model ) // Function naming rules: Start With smarty_modifier _
{
Switch ( $ Model ){
Case "Lower ":
$ Str =Strtolower ( $ Str );
Break ;
Case "Upper ":
$ Str = Strtoupper ( $ Str );
Break ;
Case "First_upper ":
$ Str = Ucfirst ( $ Str );
Break ;
}
Return $ Str ;
}

?>
<? PHP
/*
* Smarty plugin
* Custom string truncation Function
*/
Function Smarty_modifier_jiequ ( $ String , $ Start = 0, $ Length = 100 ){// Three parameters: $ string specifies the string to be intercepted, $ start specifies the start of the truncation, and $ length specifies the length of the truncation.
Return $ String = Substr ( $ String , $ Start , $ Length );
}
?>

With the above preparations, we can start to use smarty to operate string variables in the template ~~~

① First, initialize the smarty

<? PHP
Include"./Smarty. ini. php ";
$ Test= "ABC def djddk wdjli ";//Can be obtained from the database as needed
$ TPL-> Assign ("test ",$ Test);
$ TPL-> Display ("test2.html ");
?>

In the ②test2.html fileCodeAs follows:

 <  Table  Style  = "Text-align: Right; Background: # ecffec; float: left"  > 
< Tr Align = "Center" > < Th Colspan = "2" > Use a custom smarty plug-in function to operate strings </ Th > </ Tr >
< Tr > < TD > Original character string:</ TD > < TD > < {$ Test } > < BR > </ TD > </ Tr >
< Tr > < TD > Big write: </ TD > < TD > < {$ Test | Stredit: Upper} > < BR > </ TD > </ Tr >
< Tr > < TD > Write: </ TD > < TD > < {$ Test | Stredit: lower} > < BR > </ TD > </ Tr >
< Tr > < TD > Uppercase letters: </ TD > < TD > < {$ Test | Stredit: first_upper} > < BR > </ TD > </ Tr >
< Tr > < TD > Truncation string: </ TD > < TD > < {$ Test | Jiequ: "0": "5 "} > < BR > </ TD > </ Tr >
< Tr > < TD > Uppercase and then truncated: </ TD > < TD > < {$ Test | Stredit: Upper | jiequ: "0": "5 "} > < BR > </ TD > </ Tr >
< Tr > < TD > First lower case followed by upper case: </ TD > < TD > < {$ Test | Stredit: lower | stredit: first_upper} > < BR > </ TD > </ Tr >
</ Table >
 
 

< Table Style = "Text-align: Right; Background: # ffe0c1; float: Left; margin-left: 10px" >
< Tr Align = "Center" > < Th Colspan = "2" > Use a system-defined function to operate a string</ Th > </ Tr >
< Tr > < TD > Original character string: </ TD > < TD > < {$ Test } > < BR > </ TD > </ Tr >
< Tr > < TD > Big write: </ TD > < TD > < {$ Test | Upper} > < BR > </ TD > </ Tr >
< Tr > < TD > Write: </ TD > < TD > < {$ Test | Lower} > < BR > </ TD > </ Tr >
< Tr > < TD > Uppercase letters: </ TD > < TD > < {$ Test | Capitalize} > < BR > </ TD > </ Tr > <! -- This function is used to uppercase the first character of all words. -->
< Tr > < TD > Truncation string: </ TD > < TD > < {$ Test | Truncate: 5: "": true} > < BR > </ TD > </ Tr >
< Tr > < TD > Uppercase and then truncated: </ TD > < TD > < {$ Test | Upper | truncate: 5: "": true} > < BR > </ TD > </ Tr >
< Tr > < TD > First lower case followed by upper case: </ TD > < TD > < {$ Test | Lower | capitalize} > < BR > </ TD > </ Tr >
</ Table >

③ Output result

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.