PHP template engine smarty Built-in variable mediator usage detailed _php instance

Source: Internet
Author: User
Tags lowercase php template smarty template

This example describes the PHP template engine smarty built-in variable mediator usage. Share to everyone for your reference, specific as follows:

The variable mediator in Smarty is equivalent to a function, which is called by the following "|" directly to the mediator function name, if there are parameters, add in ":" After the words, Add.

The following is an introduction to the variable mediators built into the Smarty:

1, capitalize

Capitalize all the words in the variable in the first word. The parameter value Boolean determines the word with numbers and whether the first word is uppercase. Default does not write

index.php

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

index.html (template file)

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

The result: Hello World wor2ld!!!、 Hello World wor2ld!!!

2, Count_characters

Calculates the number of characters in a variable, which does not calculate spaces by default (spaces, tabs, carriage returns ...). ) only counts the number of characters, and can be very good for Chinese character calculation, and if you add a parameter true, the space is computed.

Index.html

<{$str |count_characters}>//No space
<{calculated $str |count_characters:true}>//Space

The results are: 13, 14

3, Cat

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

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

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

4, Count_paragraphs

Calculate the number of segments, calculate the number of paragraphs in the variable, perfect support for Chinese paragraphs.

index.php

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

Index.html

<{$str |count_paragraphs}>

Results are: 3

5, Count_sentences

Calculate 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}>

Results are: 2

6, Count_words

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

index.php

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

Index.html

<{$str |count_words}>

Results are: 12

7, Date_format

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

index.php

$tpl->assign (' Date ', Time ()); Passing Time stamps

Index.html

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

Results: 2012-01-26 14:37:22

8, default

By default, sets a default value 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 ', '); Assignment 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, conversion of single quotes, hexadecimal transcoding, hex beautification, or JavaScript transcoding on variables without transcoding, default is HTML transcoding

index.php

$html = <<google
html;
$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 results are:

HTML transcoding: Google
URL Transfer code: HTTP%3A%2F%2FWWW.GOOGLE.COM.HK
JS transcoding:

10, indent

Indents, indented strings per line, the first parameter specifies how many strings to indent, the default is four characters, and the second argument specifies what character the indentation replaces.

11, lower

lowercase, the variable string is lowercase.

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

12, Upper

Capitalize, and change the variable to uppercase.

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

13, NL2BR

newline character replaced by

All line breaks will be replaced with PHP's NL2BR () function.

14, Regex_replace

Regular substitutions, looking for and replacing regular expressions, like 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 strings.

16, Spacify

Interpolation, interpolation (do not know what the meaning of the word, as the name implies ^ ^) is a string of characters between each character inserted space or other characters (string).

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 of formatting floating-point numbers, such as decimal digits. Format with 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 duplicate spaces, wrap, tab to 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:

Grandmother of eight makes hole in one.

19, Strip_tags

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

index.php

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

Index.html

<{$str |strip_tags}>

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

20, truncate

Intercept to intercept the beginning of a string. The default is 80, you can specify the second argument as the character after the intercepted string, by default, Smarty is intercepted to the end of a word, and if you want to accurately intercept the number of characters, change the third argument to "true".

index.php

Copy Code code as follows:
$TPL->assign (' str ', ' Once there 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 ...

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.

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.