Use Smarty in PHP 4: user-defined variable mediator _ PHP Tutorial

Source: Internet
Author: User
Use the Smarty 4: custom variable mediation in PHP. 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. in PHP, there are many text processing functions. 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)

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)

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

Variable mediation in Smarty

<{$ Str}>

<{$ Str1}>

<{$ Str2}>

<{$ Str3}>

<{$ Str4}>

<{$ Str5}>

<{$ Str | transform}>

<{$ Str | transform: "lower"}>

<{$ Str | transform: "upper"}>

<{$ Str | transform: "firstdx"}>

<{$ Str | subString: 0: 15: "###"}>

<{$ Str | subString: 0: 15: "@" | transform: "upper"}>

<{$ Str | transform: "upper" | subString: 0: 15: ""}>

/Libs/plugins/modifier. transform. php (file converter)

/**

* 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)

/**

* 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

There are a lot of text processing functions in the handler. you can use the function to process the text and then call assign () in the Smarty template engine to assign values to the variable, allocate to template...

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.