This example describes the PHP template engine smarty Custom variable mediator usage. Share to everyone for your reference, specific as follows:
In PHP, there are many functions that handle text, you can use the text to be processed after the function processing, and then call the Smarty template engine assign () assignment to the variable, assigned to the template to display.
The variable mediator in Smarty is similar to the function in PHP that handles text, but the syntax is not the same, in the Smarty, it is through the "|" Directly after the mediator function name, if there are parameters, add to ":" After the words, add up.
Format: {$var |modifier1: "Parameter 1": "Parameter 2": Parameter 3|modifier2|modifier3| ...}
The file that defines the mediator must be placed in Smarty, the specific path is: libs/plugins/. Its file name must be modifier in Smarty format. Mediator name. php
The following example demonstrates the use of a custom variable mediator in Smarty
Program idea: Do two variable mediator, function is: a converted text; an intercept text.
init.inc.php (smarty initialization file)
<?php
define (' Root_path ', DirName (__file__));//Set the 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 (main 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 assigned to str
$_tpl->assign (' str1 ', Strtolower ($_STR)); The strings are all converted to lowercase str1
$_tpl->assign (' str2 ', Strtoupper ($_STR));//string all converted to uppercase to str2
$_tpl->assign (' STR3 ' , Ucfirst ($_STR)); The first letter of the string is converted to uppercase to STR3
$_tpl->assign (' Str4 ', substr ($_str, 0,15). ...'); The first 15 characters of the string are intercepted, followed by ' ... ' instead and assigned to STR4
$_tpl->assign (' Str5 ', Strtoupper (substr, $_str)). ' ...'); The first 15 characters of the Intercept string are converted to uppercase, followed by ' ... ' instead, and assigned to STR4
$_tpl->display (' index.tpl ');//Introduce template
?>
Tpl/index.tpl
/libs/plugins/modifier.transform.php (convert file mediator)
<?php
/** *
smarty_modifier_transform
* String conversion variable mediator function
* @param string $string process 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 (intercepting text mediator)
<?php
/** *
smarty_modifier_substring
* Processing intercept string Mediator
* @param string $string processing strings
* @param int $start _num start position, default start from scratch
* @param int $end _num end position, default
* @param string $addTo append string, 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 results:
The above example indicates that the mediator file must be placed under the Smarty plug-in directory plugins, and that the naming must follow the Smarty rules so that you can invoke the mediator function that you wrote. There is also a point 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, cannot place multiple.
Well, the custom mediator is first introduced here, and there are a lot of Smarty that have been written, which can be invoked directly or modified to the style you like. For the Smary built-in mediator, the following chapters will be covered in detail.
More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on Smarty template.