PHP uses Smarty IV: Custom variable Mediator _php tutorial

Source: Internet
Author: User
Tags smarty template truncated
In PHP, there are many functions for working with text, you can use the text to be processed through the function, then call the Smarty template engine assign () assignment to the variable, assigned to the template for display.

The variable mediator in Smarty is similar to the function in PHP for working with text, but the syntax is not the same, in Smarty, it is through the "|" directly followed by the function name of the mediator, if there are parameters, added to ":" After a number of parameters, the sum can be.

Format: {$var |modifier1: "Parameter 1": "Parameter 2": Parameter 3|modifier2|modifier3| ...}

The file that defines the mediator must be placed in the smarty, with the following path: libs/plugins/. Its file name must be modifier in the smarty format. The name of the mediator. PHP

The following example shows the use of a custom variable mediator in Smarty

Program ideas: Do two variable mediator, the function is: a converted text; a truncated text.

init.inc.php (smarty initialization file)

Define (' Root_path ', DirName (__file__)); Set the Web site root directory

Require Root_path. ' /libs/smarty.class.php '; Loading the Smarty template engine

$_TPL = new Smarty (); Create an Instance Object

$_tpl->template_dir = Root_path. ' /tpl/'; Set Template file directory

$_tpl->compile_dir = Root_path. /com/'; Set compilation file directory

$_tpl->left_delimiter = ' <{';//Set Left delimiter

$_tpl->right_delimiter = '}> '; Set Right delimiter

?>

index.php (Master file)

Define (' Const_var ', ' ABC ');

Require ' init.inc.php '; Introduction of template initialization files

Global $_TPL;

$_str = ' Abcdefghigklmnopqrstuvwsyz '; Define a string

$_tpl->assign (' str ', $_STR); String assignment to Str

$_tpl->assign (' str1 ', Strtolower ($_STR)); All strings are converted to lowercase and assigned to STR1

$_tpl->assign (' str2 ', Strtoupper ($_STR)); All strings are converted to uppercase to STR2

$_tpl->assign (' Str3 ', Ucfirst ($_STR)); All strings are converted to uppercase to STR3

$_tpl->assign (' Str4 ', substr ($_str, 0,15). ' .//Intercept The first 15 characters of the string, followed by ' ... ' instead, and assign to STR4

$_tpl->assign (' Str5 ', Strtoupper (substr ($_str, 0,15)). ' ...'); The first 15 characters of the truncated string are converted to uppercase, followed by ' ... ' instead, and assigned to STR4

$_tpl->display (' Index.tpl '); Introducing Templates

?>

Tpl/index.tpl

Variable mediators in the Smarty

<{$str}>

<{$str 1}>

<{$str 2}>

<{$str 3}>

<{$str 4}>

<{$str 5}>

<{$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 (convert file mediator)

/**

* Smarty_modifier_transform

* Variable mediator function for string conversions

* @param string $string processing strings

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

/**

* smarty_modifier_substring

* Processing intercept string mediator

* @param string $string processing strings

* @param int $start _num start position, default from start

* @param int $end _num End position, default 20

* Append string @param string $addTo, default ' ... '

*/

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 shows that the mediator file must be placed under the Smarty plug-in directory plugins, and the name must follow the Smarty rules, so that the mediator function you write can be called. It is also necessary to note that the defined function name must also conform to the Smarty default naming rules, such as: Smarty_modifier_xxx, and a mediator file, can only put a function, can not be placed more than one.

Well, the custom mediator first introduced here, Smarty has a lot of written mediation functions, you can directly call or modify the style you like. About the Smary built-in mediator, the following section describes


Excerpt from: Lee's Column

http://www.bkjia.com/PHPjc/478570.html www.bkjia.com true http://www.bkjia.com/PHPjc/478570.html techarticle in PHP, there are many functions for working with text, you can manipulate the text through the function, then call the Smarty template engine assign () assignment to the variable, assigned to the template ...

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