PHP template engine Smarty custom variable mediation usage

Source: Internet
Author: User
Tags php define php template
This article mainly introduces the usage of the PHP template engine Smarty custom variable mediation, and analyzes in detail the usage of the smarty variable regulator and the implementation skills of the custom variable regulator, for more information about the usage of the PHP template engine Smarty custom variable mediation, see the following example. We will share this with you for your reference. The details are as follows:

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 _); // Set the website ROOT directory 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/'; // Set the compiling file directory $ _ tpl-> left_delimiter =' <{'; // Set the left delimiter $ _ tpl-> right_delimiter ='}> '; // set 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); // The string is assigned to str $ _ tpl-> assign ('str1', strtolower ($ _ str )); // All strings are converted to lowercase and assigned to str1 $ _ tpl-> assign ('str2', strtoupper ($ _ str )); // Convert all strings to uppercase and assign them to str2 $ _ tpl-> assign ('str3', ucfirst ($ _ str )); // converts the initial character string to uppercase and assigns it to str3 $ _ tpl-> assign ('str4', substr ($ _ str ). '... '); // Intercept The first 15 characters of the string, followed '... 'Replace and assign it to str4 $ _ tpl-> assign ('str5', strtoupper (substr ($ _ str )). '... '); // The first 15 characters of the truncated string are converted to uppercase, followed '... ', and assign it 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)

<? 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 and intercept the string mediation device * @ param string $ string process the string * @ param int $ start_num start position. the default start position is * @ param int $ end_num, the default value is 20 * @ param string $ addTo. 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. For more information about the built-in mediation tool of Smary, see the following sections.

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.