Variable operators of the php template engine smarty

Source: Internet
Author: User
The variable operator of the php template engine smarty can be used to operate variables, user-defined functions, and characters. The variable operator of the php template engine smarty can be used to operate variables, user-defined functions, and characters.
The syntax uses "|" to apply the variable operator. for multiple parameters, use ":"?? Refers to memory? /DIV>
Capitalize [uppercase letters]
Count_characters [count characters]
Cat [connection string]
Count_paragraphs [calculate the number of paragraphs]
Count_sentences [calculate the number of periods]
Count_words [count words]
Date_format [time format]
Default [default]
Escape [transcoding]
Indent [indent]
Lower [lower case]
Nl2br [replace line breaks
]
Regex_replace [regular expression replacement]
Replace [replace]
Spacify [blank]
String_format [string formatting]
Strip [remove (extra space)]
Strip_tags [remove html tags]
Truncate [truncate]
Upper [uppercase]
Wordwrap [row width constraints]
Use multiple operators in combination
Instance:
{* Uppercase title *}
{$ Title | upper} {* take the first 40 characters *}
Topic: {$ topic | truncate: 40: "..."} {* format text string *}
{"Now" | date_format: "% Y/% m/% d"} {* apply the regulator in a custom function *}
{Mailto | upper address = "main@php118.com "}
Capitalize (uppercase)
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'Police begin campaign to rundown jaycyclers .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle}
{$ ArticleTitle | capitalize}
 
OUTPUT:
 
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
Count_characters (count the characters in the variable) index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'Cold Wave Linked to Temperatures .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle}
{$ ArticleTitle | count_characters}
 
OUTPUT:
 
Cold Wave Linked to Temperatures.
32
Cat (connection string)
Connect the value in cat to the end of the given variable
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'sychics predict world did' t end ');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle | cat: "yesterday ."}
 
OUTPUT:
 
Psychics predict world didn't end yesterday. count_paragraphs (number of segments)
Calculates the number of paragraphs in a variable.
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle}
{$ ArticleTitle | count_paragraphs}
 
OUTPUT:
 
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
 
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2count_sentences (calculate the number of periods)
Calculates the number of sentences in a variable.
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'Two Soviet Ships Collide-One Dies. Enraged Cow Injures Farmer with Axe .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle}
{$ ArticleTitle | count_sentences}
 
OUTPUT:
 
Two Soviet Ships Collide-One Dies. Enraged Cow Injures Farmer with Axe.
2
Count_words)
Calculate the number of words in a variable
Index. php: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'dealers' Will Hear Car Talk at Noon .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle}
{$ ArticleTitle | count_words}
 
OUTPUT:
 
Dealers Will Hear Car Talk at Noon.
7
Date_format (date format)
Parameter Position
Parameter location Type Required Default Description
1 string No % B % e, % Y This is the format for the outputted date.
Output string format
2 string No n/a This is the default date if the input is empty.
When the input is null, the format date and time are set in the specified function serftime (); by default.
Timestamp (parsable by strtotime) of Unix or mysql can be passed to smarty.
The designer can use date_format to completely control the date format.
If the data passed to date_format is empty, the second parameter is used as the time format.
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('Yesterday', strtotime ('-1 DAY '));
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ Smarty. now | date_format}
{$ Smarty. now | date_format: "% A, % B % e, % Y "}
{$ Smarty. now | date_format: "% H: % M: % S "}
{$ Yesterday | date_format}
{$ Yesterday | date_format: "% A, % B % e, % Y "}
{$ Yesterday | date_format: "% H: % M: % S "}
 
OUTPUT:
 
Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14: 33: 00 default (default)
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
This is the default output when the variable is empty.
When the variable is null or unallocated, the output is replaced by the given default value.
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'dealers' Will Hear Car Talk at Noon .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
 
{$ ArticleTitle | default: "no title "}
{$ MyTitle | default: "no title "}
 
OUTPUT:
 
Dealers Will Hear Car Talk at Noon.
No titleescape (transcoding)
Parameter Position Type Required Possible Values Default Description
1 string No html, htmlall, url, quotes, hex, hexentity, javascript html This is the escape format to use.
It is used for html transcoding and url transcoding. it is used to convert single quotes, hexadecimal transcoding, hexadecimal beautification, or javascript transcoding on variables without transcoding.
The default format is html transcoding.
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "'stiff Opposition Expected to Casketless Funeral plan'"); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | escape: "html"} {* escapes & "'<> *} {$ articleTitle | escape: "htmlall"} {* escapes ALL html entities *} {$ articleTitle | escape: "url"} {$ articleTitle | escape: "quotes"} href = "{$ EmailAddress | escape:" hexentity "} mailto: {$ EmailAddress | escape:" hex "}" >{$ EmailAddress | escape: "hexentity"} OUTPUT: 'stiff Opposition Expected to Casketless Funeral plan' 'Stiff % 20 Opposition % 20 Expected % 20to % 20 Casketless % 20 Funeral % 20Plan ''Stiff % 20 Opposition % 20 Expected % 20to % 20 casketless % 20 Funeral % 20Plan ''Stiff % 20 Opposition % 20 Expected % 20to % 20 Casketless % 20 Funeral % 20Plan'' Stiff + Opposition + Expected + to + Casketless + Funeral + Plan ''stiff Opposition Expected to Casketless Funeral Plan 'href = "bob@me.netmailto: % 62% 6f % 62% 40% 6d % 65% 2e % 6e % 65% 74 "> bob@me.netindent (indent)
Parameter Position Type Required Default Description
1 integer No 4 This determines how character characters to indent.
2 string No (one space) This is the character used to indent with. indent the string in each line. the default value is 4 characters (also in pear standard ).
As an optional parameter, you can specify the number of indentations.
As the Second Optional parameter, you can specify the characters used to replace indentation.
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', 'NJ judge to rule on nude beach. '); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | indent: 10} {$ articleTitle | indent: 1: "t"} OUTPUT: NJ judge to rule on nude beach. sun or rain expected today, dark tonight. statistics show that teen pregnancy drops off significantly after 25. NJ judge to rule on nude beach. sun or rain expected today, dark tonight. statistics show that teen pregnancy drops off significantly after 25.NJ judge to rule on nude beach. sun or rain expected today, dark tonight. statistics show that teen pregnancy drops off significantly after 25. NJ judge to rule on nude beach. sun or rain expected today, dark tonight. statistics show that teen pregnancy drops off significantly after 25.
Lower (lower case)
Lowercase variable string
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', 'Two Convicts Evade Noose, Jury Hung. '); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | lower} OUTPUT: Two Convicts Evade Noose, Jury Hung. two convicts evade noose, jury hung.
Nl2br (replace linefeed
)
All linefeeds will be replaced
. Same as the nl2br () function in php.
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "Sun or rain expectedntoday, dark tonight"); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle | nl2br} OUTPUT: Sun or rain expected
Today, dark tonight
Regex_replace (regular expression replacement)
Find and replace regular expressions.
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
Replace the regular expression.
 
2 string Yes n/a This is the string of text to replace.
Which text string is used to replace index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "Infertility unlikely tonbe passed on, experts say. "); $ smarty-> display ('index. tpl '); index. tpl: {* replace each carriage return, tab & new line with a space *} {* replace each carriage return with a space, tab, and line break *} {$ articleTitle | regex_replace: "/[rtn]/": ""} OUTPUT: Infertility unlikely tobe passed on, experts say. infertility unlikely to be passed on, experts say.
Replace)
Simple search and replacement of strings
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
String to be replaced
2 string Yes n/a This is the string of text to replace.
Text index to replace. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "Child's Stool Great for Use in Garden. "); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | replace: "Garden": "Vineyard" }{$ articleTitle | replace: "": ""} OUTPUT: child's Stool Great for Use in Garden. child's Stool Great for Use in Vineyard. child's Stool Great for Use in Garden. spacify
Is to insert spaces or other characters (strings) between each character of a string ).
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', 'something Went Wrong in Jet Crash, Experts Say. '); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | spacify: "^"} OUTPUT: Something Went Wrong in Jet Crash, Experts Say. S o m e t h I n g W e n t W r o n g I n J e t C r a s h, E x p e r t s a y. S ^ o ^ m ^ e ^ t ^ h ^ I ^ n ^ g ^ W ^ e ^ n ^ t ^ ^ W ^ r ^ o ^ n ^ g ^ I ^ n ^ J ^ e ^ t ^ C ^ r ^ a ^ s ^ h ^, ^ E ^ x ^ p ^ e ^ r ^ t ^ s ^ S ^ a ^ y ^.
String_format (string formatting)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
The format used is a method for formatting floating-point numbers. for example, decimal number. use the sprintf syntax to format the index. php: $ smarty = new Smarty; $ smarty-> assign ('number', 23.5787446); $ smarty-> display ('index. tpl '); index. tpl: {$ number} {$ number | string_format: "%. 2f "} {$ number | string_format:" % d "} OUTPUT: 23.578744623.5824
Strip (remove (extra space)
Replace all repeated spaces. The line feed and tab are single.
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "Grandmother ofneight makest hole in one. "); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | strip: ""} OUTPUT: Grandmother ofeight makes hole in one. grandmother of eight makes hole in one. grandmother of eight makes hole in one.
Strip_tags (remove html tags)
Remove <和> Between all labels, including <和> .
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "Blind Woman Gets New Kidney from Dad she hasn' t Seen in Years. "); $ Smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | strip_tags} OUTPUT: Blind Woman Gets New Kidney from Dad she Hasn't Seen in Years. Blind Woman Gets New Kidney from Dad she Hasn't Seen in years. truncate (screenshot)
Parameter Position Type Required Default Description
1 integer No 80 This determines how character characters to truncate.
Specifies the number of characters to intercept.
2 string No... This is the text to append if truncation occurs.
Truncates the string followed by the intercepted word.
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true ).
Check whether the part starting from the string is intercepted at the boundary of the word. the default value is 80.
You can specify the second parameter as the character after the string to be intercepted.
By default, smarty captures the end of a word,
If you want to accurately intercept the number of characters, change the third parameter to "true" index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter. '); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | truncate: 30} {$ articleTitle | truncate: 30: ""} {$ articleTitle | truncate: 30: "---"} {$ articleTitle | truncate: 30: "": true} {$ articleTitle | truncate: 30 :"... ": true} OUTPUT: Two Sisters Reunite after Eighteen Years at Checkout Counter. two Sisters Reunite after Eighteen Years at Checkout Counter. two Sisters Reunite after... two Sisters Reunite afterTwo Sisters Reunite after --- Two Sisters Reunite after EighTwo Sisters Reunite after E...
Upper (uppercase)
Change the variable to uppercase
Index. php: $ smarty = new Smarty; $ smarty-> assign ('articletitle', "If Strike isn' t Settled Quickly it may Last a While. "); $ smarty-> display ('index. tpl '); index. tpl: {$ articleTitle} {$ articleTitle | upper} OUTPUT: If Strike ISN'T Settled Quickly it may Last a While. if strike isn't settled quickly it may last a while.
Wordwrap (row width constraint)
You can specify the width of a paragraph (that is, the number of characters in a row, exceeding the number of characters in a line). The default value is 80.
The second parameter is optional. you can specify the characters used at the constraint point (the default is the linefeed n ).
By default, smarty captures the end of a word. you can specify the exact number of characters to intercept.
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap.
Specifies the width of a paragraph (sentence ).
2 string No n This is the string used to wrap words.
What character constraints are used
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true ).
Exact constraint to character index. php:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', "Blind woman gets new kidney from dad she hasn' t seen in years .");
$ Smarty-> display ('index. tpl ');
Index. tpl:
{$ ArticleTitle}
{$ ArticleTitle | wordwrap: 30}
{$ ArticleTitle | wordwrap: 20}
{$ ArticleTitle | wordwrap: 30 :"
N "}
{$ ArticleTitle | wordwrap: 30: "n": true}
OUTPUT:
Blind woman gets new kidney from dad she hasn't seen in years.
Blind woman gets new kidney
From dad she hasn' t seen in
Years.
Blind woman gets new
Kidney from dad she
Hasn' t seen in
Years.
Blind woman gets new kidney

From dad she hasn't seen in years.
Blind woman gets new kidney fr
Om dad she hasn't seen in year
S. use multiple operators in combination
You can apply operators to a variable. they are combined from left to right. multiple operators must be separated by the "|" symbol.
Index. php:
 
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'Smokers are Productive, but Death Cuts Efficiency .');
$ Smarty-> display ('index. tpl ');
 
Index. tpl:
{$ ArticleTitle}
{$ ArticleTitle | upper | spacify}
{$ ArticleTitle | lower | spacify | truncate}
{$ ArticleTitle | lower | truncate: 30 | spacify}
{$ ArticleTitle | lower | spacify | truncate: 30 :"..."}
 
 
OUTPUT:
 
Smokers are Productive, but Death Cuts Efficiency.
S m o k e r s a r e p r o d u c t I V E, B u t d e a t h c u t s e f I c I E N C Y.
S m o k e r s a r e p r o d u c t I v e, B u t d e a t h c u t s...
S m o k e r s a r e p r o d u c t I v e, B u t...
S m o k e r s a r e p...

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.