There are many text processing functions in PHP. You can use the function to process the text to be processed, and then call assign () in the Smarty template engine to assign values to the variable, assigned to the template for display.
The variable mediation in Smarty is similar to the function used to process text in PHP, but the syntax is different. In Smarty, the variable mediation is directly followed by the mediation function name through "|". If there is a parameter, it must be added after ":". If there are multiple parameters, add them.
Format: {$ var | modifier1: "parameter 1": "parameter 2": parameter 3 | modifier2 | modifier3 | ...}
Files defining the mediation must be placed in Smarty. The specific path is libs/plugins /. The file name must be in the format of modifier. Mediation device name. php In the Smarty format.
The following example shows how to use the custom variable mediation in Smarty.
Program idea: it is used as a variable mediation device to convert text and intercept text.
Init. inc. php (Smarty initialization file)
<? Php
Define ('root _ path', dirname (_ FILE _); // you can specify the ROOT directory of a website.
Require ROOT_PATH. '/libs/Smarty. class. php'; // load the Smarty template engine
$ _ Tpl = new Smarty (); // create an Instance Object
$ _ Tpl-> template_dir = ROOT_PATH. '/tpl/'; // set the template file directory
$ _ Tpl-> compile_dir = ROOT_PATH. './com/'; // you can specify the directory for compiling files.
$ _ Tpl-> left_delimiter = '<{'; // set the left delimiter.
$ _ Tpl-> right_delimiter = '}>'; // you can specify the right delimiter.
?>
Index. php (main file)
<? Php
Define ('const _ var', 'abc ');
Require 'init. inc. php'; // introduce the template Initialization File
Global $ _ tpl;
$ _ Str = 'abcdefghigklmnopqrstuvwsyz'; // define a string
$ _ Tpl-> assign ('str', $ _ str); // assign a string to str
$ _ Tpl-> assign ('str1', strtolower ($ _ str); // convert all strings to lowercase and assign them to str1
$ _ Tpl-> assign ('str2', strtoupper ($ _ str); // convert all strings to uppercase and assign them to str2.
$ _ Tpl-> assign ('str3', ucfirst ($ _ str); // convert all strings to uppercase and assign them to str3
$ _ Tpl-> assign ('str4', substr ($ _ str, 0, 15 ). '... '); // capture the first 15 characters of the string, followed '... 'instead, and assigned to str4
$ _ Tpl-> assign ('str5', strtoupper (substr ($ _ str, 0, 15 )). '... '); // the first 15 characters of the truncated string are converted to uppercase, followed '... 'instead, and assigned to str4
$ _ Tpl-> display ('index. tpl'); // introduce the Template
?>
Tpl/index. tpl
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> variable mediation in Smarty </title>
</Head>
<Body>
<{$ Str }>< br/> <! -- Output string prototype -->
<{$ Str1 }>< br/> <! -- Convert the output string to lowercase -->
<{$ Str2 }>< br/> <! -- Convert the output string to uppercase -->
<{$ Str3 }>< br/> <! -- Uppercase letters of the output string -->
<{$ Str4 }>< br/> <! -- Intercept the string and intercept 15 strings. Use '...' to replace the string. -->
<{$ Str5}> <br/> <! -- Truncates a string, truncates 15 strings, converts them to uppercase, and replaces them with '...'. -->
<! --
You will find that using the assigned variables is not very flexible,
To process the string, you must create a new variable in the main file and assign the value to the new variable.
Variable mediation can be used to flexibly process strings.
-->
<{$ Str | transform}> <br/> <! -- Output string prototype -->
<{$ Str | transform: "lower" }>< br/> <! -- Convert the output string to lowercase -->
<{$ Str | transform: "upper" }>< br/> <! -- Convert the output string to uppercase -->
<{$ Str | transform: "firstdx" }>< br/> <! -- Uppercase letters of the output string -->
<{$ Str | subString: 0: 15: "###" >>< br/> <! -- Intercept a string and intercept 15 strings. Use '###' to replace the string. -->
<{$ Str | subString: 0: 15: "@" | transform: "upper" }>< br/> <! -- Intercept the string and intercept 15 strings. Replace the string with '@'. -->
<{$ Str | transform: "upper" | subString: 0: 15: "" >>< br/> <! -- Functions are the same as above -->
</Body>
</Html>
/Libs/plugins/modifier. transform. php (File Converter)
<? Php
/**
* Smarty_modifier_transform
* Variable mediation function for String Conversion
* @ Param string $ string processing string
* @ Param string $ type processing type
*/
Function smarty_modifier_transform ($ string, $ type ){
Switch ($ type ){
Case 'upper ':
$ Str = strtoupper ($ string );
Break;
Case 'lower ':
$ Str = strtolower ($ string );
Break;
Case 'firstdx ':
$ Str = ucfirst ($ string );
Break;
Default:
$ Str = $ string;
}
Return $ str;
}
?>
Lib/plugins/modifier. subString. php (intercept text mediation)
<? Php
/**
* Smarty_modifier_subString
* Process the intercepted string mediation
* @ Param string $ string processing string
* @ Param int $ start_num start position, starting from scratch by default
* @ Param int $ end_num end position. The default value is 20.
* @ Param string $ addTo append a string. The default value is '...'
*/
Function smarty_modifier_subString ($ string, $ start_num = 0, $ end_num = 20, $ addTo = '...'){
$ _ Str = '';
If (strlen (substr ($ string, $ start_num, $ end_num) >=$ end_num ){
$ _ Str = substr ($ string, $ start_num, $ end_num). $ addTo;
} Else {
$ _ Str = substr ($ string, $ start_num, $ end_num );
}
Return $ _ str;
}
?>
Execution result:
The above example indicates that the Mediation file must be placed under the plug-in directory plugins of Smarty, and the name must follow the Smarty rules. In this way, you can call the mediation function you have compiled. Note that the name of the defined function must also comply with the naming rules set in Smarty, for example, smarty_modifier_XXX, and only one function can be placed in one Mediation file, but not multiple functions.
Now, the User-Defined mediation tool is introduced here. There are many already written mediation functions in Smarty, which can be called directly or modified to your favorite style. Next section describes the built-in mediation tool of Smary.
From: Lee.'s column