PHP template engine Smarty built-in variable mediator usage detailed, template smarty_php tutorial

Source: Internet
Author: User
Tags php template smarty template

PHP template engine Smarty built-in variable mediator usage detailed, template Smarty


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

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 =
 
  

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 = <

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 = <

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 = <
 
      < for="" (var="" i="0;"><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, including <和> .

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 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 ...

More about PHP related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary

It is hoped that this article will be helpful to everyone based on smarty template PHP program design.

Articles you may be interested in:

    • PHP template engine Smarty built-in functions
    • PHP template engine smarty custom variable mediator usage
    • Analysis of the usage of reserved variables in PHP template engine Smarty
    • PHP template engine Smarty built-in function foreach,foreachelse usage analysis
    • PHP template Engine Smarty Example of how a configuration file is used in template variables
    • Example of how variables are used in PHP template engine Smarty
    • Smarty how the template engine obtains data from PHP
    • thinkphp How to use the Smarty template engine
    • A method of generating random numbers in the PHP template engine smarty and an explanation of the math function
    • Summary of cache Usage for PHP template engine Smarty
    • 6 Tips for PHP smarty template engine
    • [PHP] Template engine Smarty Introduction
    • PHP template engine Smarty built-in functions section,sectionelse usage

http://www.bkjia.com/PHPjc/1119972.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119972.html techarticle PHP template engine smarty built-in variable mediator usage details, template Smarty This example describes the PHP template engine Smarty built-in variable mediator usage. Share to everyone for your reference, specific as ...

  • Related Article

    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.