PHP template engine Smarty built-in variable mediator usage and examples

Source: Internet
Author: User
Tags php template
This article mainly introduces the PHP template engine Smarty built-in variable mediator usage, in combination with the example form detailed analysis of the common built-in variable smarty in the definition and use of skills, the need for friends can refer to the following

The variable mediator in the Smarty is equivalent to a function, which is called by the following "|" directly followed by the function name of the mediator, if there is a parameter, added to the ":" After a number of parameters, the accumulation can be.

The following is a description of the variable mediators built into the Smarty:

1, capitalize

Capitalize the first word of all the words in the variable. The Boolean value of the parameter determines whether a word with a number is capitalized or the first word. Default not written

index.php

$TPL->assign (' str ', ' Hello World wor2ld!!! '); $tpl->display (' index.html ');

index.html (template file)

<{$str |capitalize}><{$str |capitalize:true}>

The result is: Hello world wor2ld!!!、 the Hello world wor2ld!!!

2, Count_characters

Calculates the number of characters in a variable, which by default does not count spaces (spaces, tabs, carriage returns ...). Only the number of characters is counted, and it is very good to support Chinese characters calculation; If you add the parameter true, the space is calculated.

Index.html

<{$str |count_characters}>//Do not calculate spaces <{$str |count_characters:true}>//Calculate spaces

The results are: 13, 14

3. Cat

A connection string that connects the values in cat to the given variable.

<{$str |cat: ' Happy New Year '} >

The result is: Hello World!!! Happy New Year.

4, Count_paragraphs

Calculates the number of segments, calculates the number of paragraphs in the variable, and supports the Chinese paragraph perfectly.

index.php

$str = <<assign (' str ', $str); $tpl->display (' index.html ');

Index.html

<{$str |count_paragraphs}>

The result is: 3

5, Count_sentences

Calculates the number of sentences in a variable. NOTE: Only English statements are supported and Chinese is not supported.

index.php

$str = <<assign (' str ', $STR);

Index.html

<{$str |count_sentences}>

The result is: 2

6, Count_words

Calculates the number of words and calculates the number of words in the variable.

index.php

$str = <<assign (' str ', $STR);

Index.html

<{$str |count_words}>

The result is: 12

7, Date_format

Date format, specific parameters, here are only examples of Chinese-style date format

index.php

$tpl->assign (' Date ', Time ()); Pass time stamp

Index.html

<{$date |date_format: '%y-%m-%d%h:%m:%s '}>

The result is: 2012-01-26 14:37:22

8. Default

By default, a default value is set for an empty variable, and when the variable is empty or unassigned, the output is replaced by the given default value.

index.php

$TPL->assign (' str ', '); Assign a value to null

Index.html

<{$str |default: ' Default output ... '}>, <{$string |default: ' No definition, default output ... '}>

The result is: Default output ..., no definition, default output ...

9. Escape

transcoding, for HTML transcoding, URL transcoding, converting single quotes, hex transcoding, hex beautification, or JavaScript transcoding on non-transcoding variables, HTML transcoding by default

index.php

$html = <<Googlehtml; $js = << for  (var i=0; i<100; i++) {    window.alert (i);  } JS; $tpl->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"}>

The result is:

HTML transcoding: Googleurl transcoding: Http%3a%2f%2fwww.google.com.hkjs transcoding:

10, indent

Indents, each line indents the string, the first parameter specifies how many strings are indented, the default is four characters, and the second argument, which specifies what characters the indentation uses instead.

11, lower

lowercase, the variable string is lowercase.

How to use: <{$str |lower}>

12, Upper

Uppercase, change the variable to uppercase.

How to use: <{$str |upper}>

13, NL2BR

Replace line breaks with

All line breaks will be replaced with the PHP nl2br () function.

14, Regex_replace

Regular substitution, finding and replacing regular expressions, is the same as the syntax of Preg_replace ().

index.php

$TPL->assign (' str ', ' http://www.google.com ');

Index.html

<{$str |regex_replace: '/go{2}gle/': ' Baidu '}>

The result is: http://www.baidu.com

15. Replace

Replace, simple search and replace string.

16, Spacify

Intervening spaces, intervening spaces (do not know what this word means, as the name implies ^ ^) is a string between each character insert a space or other characters (strings).

index.php

$TPL->assign (' str ', ' Hello World!!! ');

Index.html

<{$str |spacify: "^^"}>

The result: h^^e^^l^^l^ ^o^ ^ ^^ w^ ^o^ ^r^^l^^d^^! ^^! ^^!

17, String_format

String formatting, which is a method for formatting floating-point numbers, such as decimal numbers. Format using sprintf syntax.

index.php

$TPL->assign (' num ', 23.5787446);

Index.html

<{$num |string_format: "%.2f"}><{$num |string_format: "%d"}>

The results are: 23.58, 23

18, strip

Replace all repeating spaces, line breaks, tab to individual

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:

Grandmother of eight makes hole in one.

19, Strip_tags

Remove all labels between < and >, including < and >.

index.php

$TPL->assign (' str ', ' Google ');

Index.html

<{$str |strip_tags}>

The result: Google (source code is also Google, removed tags and tags)

20, truncate

Intercept, intercept the beginning of a string. The default is 80, you can specify the second parameter as the string after the truncation of what character, by default, Smarty will intercept the end of a word, if you want to accurately intercept how many characters, the third parameter to "true".

index.php

Copy the Code code as follows:

$TPL->assign (' str ', ' there once was a mountain, there was a temple on the hill. There is an old monk and a little monk in the Temple ... ');

Index.html

<{$str |truncate:10: ' ... ':true}>

The result: There used to be a mountain, a mountain ...

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.