This article mainly introduces common variable operators in Smarty. Examples summarize common variable operators, which are very useful. For more information, see
This article mainly introduces common variable operators in Smarty. Examples summarize common variable operators, which are very useful. For more information, see
This article summarizes common variable operators in Smarty and shares them with you for your reference. The details are as follows:
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
Example:
The Code is as follows:
{* 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@cn-web.com "}
Capitalize (uppercase)
The index. php page is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'police begin campaign to rundown jaycyclers .');
$ Smarty-> display ('index. tpl ');
The index. tpl page is as follows:
The Code is as follows:
{$ ArticleTitle}
{$ ArticleTitle | capitalize}
OUTPUT is as follows:
The Code is as follows:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
Count_characters (count the characters in the variable)
Index. php is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'cold Wave Linked to Temperatures .');
$ Smarty-> display ('index. tpl ');
The index. tpl page is as follows:
The Code is as follows:
{$ ArticleTitle}
{$ ArticleTitle | count_characters}
OUTPUT is as follows:
Cold Wave Linked to Temperatures.
Cat (connection string)
Connect the value in cat to the end of the given variable
Index. php is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'sychics predict world did' t end ');
$ Smarty-> display ('index. tpl ');
The index. tpl page is as follows:
The Code is as follows:
{$ ArticleTitle | cat: "yesterday ."}
OUTPUT is as follows:
The Code is as follows:
Psychics predict world didn't end yesterday.
Count_paragraphs (number of calculated segments)
Calculates the number of paragraphs in a variable.
Index. php is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday .');
$ Smarty-> display ('index. tpl ');
The index. tpl template page is as follows:
The Code is as follows:
{$ ArticleTitle}
{$ ArticleTitle | count_paragraphs}
OUTPUT is as follows:
The Code is as follows:
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
Count_sentences (calculate the number of periods)
Calculates the number of sentences in a variable.
Index. php is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'two Soviet Ships Collide-One Dies. Enraged Cow Injures Farmer with Axe .');
$ Smarty-> display ('index. tpl ');
The index. tpl template is as follows:
The Code is as follows:
{$ ArticleTitle}
{$ ArticleTitle | count_sentences}
OUTPUT is as follows:
The Code is as follows:
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 is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'dealers' Will Hear Car Talk at Noon .');
$ Smarty-> display ('index. tpl ');
The index. tpl template is as follows:
The Code is as follows:
{$ ArticleTitle}
{$ ArticleTitle | count_words}
OUTPUT is as follows:
The Code is as follows:
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.
Default setting when the input is null
Format the date and time in the given function serftime.
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 is as follows:
The Code is as follows:
$ Smarty = new Smarty;
$ Smarty-> assign ('yesterday', strtotime ('-1 Day '));
$ Smarty-> display ('index. tpl ');
Index. tpl:
The Code is as follows:
{$ 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 is as follows:
The Code is as follows: