PHP template engine Smarty custom variable mediation usage, template smarty

Source: Internet
Author: User
Tags php define php template smarty template

PHP template engine Smarty custom variable mediation usage, template smarty

This article describes the usage of the PHP template engine Smarty custom variable mediation. 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


/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.

Related Article

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: 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.