Common variable operators and Smarty operators in smarty
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 with <br/>]
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:
Copy codeCode: {* uppercase title *}
<H2 >{$ 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:
Copy codeThe 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:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | capitalize}
OUTPUT is as follows:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeCode: {$ articleTitle | cat: "yesterday ."}
OUTPUT is as follows:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | count_paragraphs}
OUTPUT is as follows:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | count_sentences}
OUTPUT is as follows:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | count_words}
OUTPUT is as follows:
Copy codeThe 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:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('yesterday', strtotime ('-1 Day '));
$ Smarty-> display ('index. tpl ');
Index. tpl:
Copy codeCode: {$ 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:
Copy codeThe Code is as follows: Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
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 null.
Set a default value for null variables.
When the variable is null or unallocated, the output is replaced by the given default value.
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'dealers' Will Hear Car Talk at Noon .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle | default: "no title "}
{$ MyTitle | default: "no title "}
OUTPUT:
Copy codeThe Code is as follows: Dealers Will Hear Car Talk at Noon.
No title
Escape (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 is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "'stiff Opposition Expected to Casketless Funeral plan '");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | escape}
{$ 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"} </a>
OUTPUT:
Copy codeThe Code is as follows: '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.net </a>
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.
Indent the string in each line. The default value is 4 characters (the pear standard is also used ).
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 is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'nj judge to rule on nude beach .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | indent}
{$ ArticleTitle | indent: 10}
{$ ArticleTitle | indent: 1: "t "}
OUTPUT:
Copy codeThe Code is as follows: 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 is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'two Convicts Evade Noose, Jury Hung .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | lower}
OUTPUT:
Copy codeThe Code is as follows: Two Convicts Evade Noose, Jury Hung.
Two convicts evade noose, jury hung.
Nl2br (replace line breaks with <br/>)
All linefeeds will be replaced with <br/>. The same as the nl2br () function in php.
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "Sun or rain expectedntoday, dark tonight ");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle | nl2br}
OUTPUT:
Copy codeThe Code is as follows: Sun or rain expected <br/> 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.
Used text string to replace
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "Infertility unlikely tonbe passed on, experts say .");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {* replace each carriage return, tab & new line with a space *} {* replace each carriage return, tab, and line feed with Spaces *}
{$ ArticleTitle}
{$ ArticleTitle | regex_replace: "/[rtn]/": ""}
OUTPUT:
Copy codeThe Code is as follows: Infertility unlikely
Be 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 to replace
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "Child's Stool Great for Use in Garden .");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | replace: "Garden": "Vineyard "}
{$ ArticleTitle | replace :"":""}
OUTPUT:
Copy codeThe Code is as follows: 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 is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'something Went Wrong in Jet Crash, Experts Say .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | spacify}
{$ ArticleTitle | spacify: "^ "}
OUTPUT:
Copy codeThe Code is as follows: 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)
Format used
It is a method for formatting floating-point numbers, such as decimal numbers. It is formatted using the sprintf syntax.
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('number', 23.5787446 );
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ number}
{$ Number | string_format: "%. 2f "}
{$ Number | string_format: "% d "}
OUTPUT:
Copy codeCode: 23.5787446
23.58
24
Strip (remove (extra space)
Replace all repeated spaces. The line feed and tab are single.
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "Grandmother ofneight makest hole in one .");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | strip}
{$ ArticleTitle | strip :""}
OUTPUT:
Copy codeThe Code is as follows: Grandmother
Eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.
Strip_tags (remove html tags)
Remove all labels between <and>, including <and>.
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle ', "Blind Woman Gets <font face =" helvetica "> New Kidney </font> from Dad she Hasn't Seen in <B> years </B>. ");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | strip_tags}
OUTPUT:
Copy codeThe Code is as follows: Blind Woman Gets <font face = "helvetica"> New Kidney </font> from Dad she Hasn't Seen in <B> years </B>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.
Truncate)
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 word boundary is intercepted
The first part of the string to be truncated. 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 is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'two Sisters Reunite after Eighteen Years at Checkout Counter .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | truncate}
{$ ArticleTitle | truncate: 30}
{$ ArticleTitle | truncate: 30 :""}
{$ ArticleTitle | truncate: 30 :"---"}
{$ ArticleTitle | truncate: 30: "": true}
{$ ArticleTitle | truncate: 30: "...": true}
OUTPUT:
Copy codeThe Code is as follows: 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 after
Two Sisters Reunite after ---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
Upper (uppercase)
Change the variable to uppercase
Index. php is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', "If Strike isn' t Settled Quickly it may Last a While .");
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | upper}
OUTPUT:
Copy codeThe Code is as follows: 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 constraints to characters
Index. php is as follows:
Copy codeThe Code is as follows: $ 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 template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | wordwrap: 30}
{$ ArticleTitle | wordwrap: 20}
{$ ArticleTitle | wordwrap: 30: "<br> n "}
{$ ArticleTitle | wordwrap: 30: "n": true}
OUTPUT:
Copy codeThe Code is as follows: 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 <br>
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.
The index. php page is as follows:
Copy codeThe Code is as follows: $ smarty = new Smarty;
$ Smarty-> assign ('articletitle', 'smokers are Productive, but Death Cuts Efficiency .');
$ Smarty-> display ('index. tpl ');
Index. tpl template:
Copy codeThe Code is as follows: {$ articleTitle}
{$ ArticleTitle | upper | spacify}
{$ ArticleTitle | lower | spacify | truncate}
{$ ArticleTitle | lower | truncate: 30 | spacify}
{$ ArticleTitle | lower | spacify | truncate: 30 :"..."}
OUTPUT:
Copy codeThe Code is as follows: 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...
I hope this article will help you with PHP programming.
In the smarty template, how does one use php variables?
This method is used.
Variable auto-increment in the smarty Template
I tried it like this:
<{Assign var = "I" value = "1"}> (outside the loop)
<{Assign var = "I" value = $ I + 1}> (increments within a loop)