PHP template engine Smarty Usage and example analysis of custom variable mediators

Source: Internet
Author: User
Tags introduction template php template smarty template truncated
This article mainly introduces the PHP template engine smarty Custom variable mediator usage, a more detailed analysis of the use of smarty variable regulator and the implementation of the custom variable regulator, the need for friends can refer to the following

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)

<?php  define (' Root_path ', DirName (__file__));//Set site root directory  require Root_path. ' /libs/smarty.class.php '; Load 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)

<?php  define (' Const_var ', ' ABC ');  Require ' init.inc.php '; Introduction of template Initialization file  global $_tpl;  $_str = ' Abcdefghigklmnopqrstuvwsyz '; Defines 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));//The string is all converted to uppercase and assigned to STR2  $_tpl->assign (' STR3 ' , Ucfirst ($_STR)); The first letter of the string is converted to uppercase and assigned 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 ');//Introduction Template?>

Tpl/index.tpl

/libs/plugins/modifier.transform.php (convert file mediator)

<?php  /**   * Smarty_modifier_transform   * String conversion variable mediator function   * @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 mediator)

<?php  /**   * smarty_modifier_substring   * Handles intercepting string mediator   * @param string $string  processing string   * @param int $start _num  start position, default from start   * @param int $end _num   end position, default   * @param string $addTo   append string, default ' ... ' c11/>*/  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.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.