PHP template engine Smarty built-in variable mediation usage details, template smarty. The usage of the PHP template engine Smarty built-in variable mediation is described in detail. the template smarty this article describes the usage of the PHP template engine Smarty built-in variable mediation. For your reference, see the usage of the built-in variable mediation tool Smarty of the PHP template engine and the template smarty.
This article describes the usage of the PHP template engine Smarty built-in variable mediation. We will share this with you for your reference. The details are as follows:
The variable mediation in Smarty is equivalent to a function. the call method is "|" followed by the name of the mediation function. if a parameter exists, it must be added after ":". if multiple parameters exist, accumulate.
The built-in variable mediation in Smarty is described as follows:
1. capitalize
Uppercase of all words in the variable. The value of the boolean type determines whether to use uppercase letters or not for words with numbers. Not capitalized by default
Index. php
$tpl->assign('str', 'hello world wor2ld!!!');$tpl->display('index.html');
Index.html (template file)
<{$str|capitalize}><{$str|capitalize:true}>
Result: Hello World wor2ld !!! Hello World Wor2Ld !!!
2. count_characters
Calculates the number of characters in the variable. by default, the mediation tool does not calculate spaces (spaces, tabs, and carriage returns ...) Only the number of characters is calculated, and Chinese characters are well supported. if the parameter is set to true, spaces are calculated.
Index.html
<{$ Str | count_characters}> // do not calculate spaces <{$ str | count_characters: true}> // calculate spaces.
Result: 13, 14
3. cat
Connects the value in cat to the end of the given variable.
<{$str|cat:' Happy new year.'}>
The result is: hello world !!! Happy new year.
4. count_paragraphs
Calculates the number of paragraphs in the variable. it perfectly supports Chinese paragraphs.
Index. php
$ Str =
Index.html
<{$str|count_paragraphs}>
Result: 3
5. count_sentences
Calculates the number of sentences in a variable. Note: Only English statements are supported and Chinese characters are not supported.
Index. php
$ Str = <Index.html
<{$str|count_sentences}>
Result: 2
6. count_words
Calculates the number of words in a variable.
Index. php
$ Str = <Index.html
<{$str|count_words}>
Result: 12
7. date_format
Date formatting. There are many specific parameters. here we only use Chinese-style date formats.
Index. php
$ Tpl-> assign ('date', time (); // transfer the timestamp
Index.html
<{$date|date_format:'%Y-%m-%d %H:%M:%S'}>
Result: 14:37:22
8. default
By default, a default value is set for null Variables. when the variable is null or not assigned, the output is replaced by the given default value.
Index. php
$ Tpl-> assign ('str', ''); // The value is null.
Index.html
<{$ Str | default: 'default output... '}>, <{$ string | default:' not defined, default output... '}>
The result is: default output..., not defined. default output...
9. escape
Transcoding: used for html transcoding and url transcoding. single quotes, hexadecimal transcoding, hexadecimal beautification, or javascript transcoding are converted to variables without transcoding. the default value is html transcoding.
Index. php
$html = <
assign('html', $html); // html$tpl->assign('url', 'http://www.google.com.hk'); // url$tpl->assign('js', $js); // javascript
Index.html
HTML Transcoding: <{$ html | escape: "html"}> URL Transcoding: <{$ url | escape: "url"}> JS Transcoding: <{$ js | escape: "javascript"}>
Result:
HTML Transcoding: GoogleURL Transcoding: http % 3A % 2F % 2Fwww.google.com. hkJS Transcoding:
10. indent
Indent: each line is indented to a string. The first parameter specifies the number of indentations. the default value is four characters. The second parameter specifies the characters used to replace indentation.
11. lower
Lowercase: lowercase variable string.
Usage: <{$ str | lower}>
12. upper
In upper case, replace the variable with upper case.
Usage: <{$ str | upper}>
13. nl2br
Replace line breaks
All linefeeds will be replaced with the nl2br () function in php.
14. regex_replace
Regular expression replacement: searches for and replaces regular expressions, which is the same as the preg_replace () syntax.
Index. php
$tpl->assign('str', 'http://www.google.com');
Index.html
<{$str|regex_replace:'/go{2}gle/':'baidu'}>
Result: http://www.baidu.com
15. replace
Replacement, simple search and replacement string.
16. spacify
Plug-in, plug-in (do not know what this word means, as the name implies ^) is a type of Insert space or other characters (strings) between each character in a string ).
Index. php
$tpl->assign('str', 'hello world!!!');
Index.html
<{$str|spacify:"^^"}>
Result: h ^ e ^ l ^ o ^ w ^ o ^ r ^ l ^ d ^! ^! ^!
17. string_format
String formatting is a method for formatting floating-point numbers, such as decimal numbers. it is formatted using sprintf syntax.
Index. php
$tpl->assign('num', 23.5787446);
Index.html
<{$num|string_format:"%.2f"}><{$num|string_format:"%d"}>
Results: 23.58, 23
18. strip
Replace all repeated spaces, line breaks, and tabs with a single
Index. php
$tpl->assign('str', "Grandmother of\neight makes\t hole in one.");
Index.html
<{$str|strip:" "}>
The result is: Grandmother of eight makes hole in one.
Source code:
Grandmother of eight makes hole in one.
19. strip_tags
Remove
<和>
Between all labels, including
<和>
.
Index. php
$tpl->assign('str', "Google");
Index.html
<{$str|strip_tags}>
The result is: Google (the source code is also Google, and tags are removed)
20. truncate
The start part of the string. the default value is 80. you can specify the second parameter as the character after the string to be intercepted. by default, smarty intercepts the string to the end of a word, if you want to accurately intercept the number of characters, change the third parameter to "true ".
Index. php
The code is as follows: $ tpl-> assign ('str', 'there was a mountain and a temple on the mountain. There is an old monk and a small monk in the temple ...');
Index.html
<{$str|truncate:10:'...':true}>
The result is: there was a mountain, mountain...