Home  >  Article  >  Backend Development  >  Summary of commonly used variable operators in Smarty, summary of smarty operators_PHP tutorial

Summary of commonly used variable operators in Smarty, summary of smarty operators_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:49939browse

A summary of commonly used variable operators in Smarty, a summary of smarty operators

This article summarizes the commonly used variable operators in Smarty and shares them with you for your reference. The details are as follows:

The variable operator of PHP template engine smarty can be used to operate variables, custom functions and characters.
Use "|" in the syntax to apply the variable operator, and use ":" for multiple parameters?/DIV>

capitalize[capitalize]
count_characters[Count the number of characters]
cat[connection string]
count_paragraphs[Count number of paragraphs]
count_sentences[Count sentences]
count_words[Count the number of words]
date_format[time format]
default[default]
escape[transcoding]
indent[indent]
lower[lowercase]
nl2br[Line breaks replaced with
]
regex_replace[regular replacement]
replace[replace]
spacify[insert blank]
string_format[string format]
strip[remove (excess spaces)]
strip_tags[remove html tags]
truncate[interception]
upper[uppercase]
wordwrap[line width constraint]
Combining multiple operators

Examples are as follows:

Copy the code The code is as follows:
{*capitalize the title*}

{$title|upper}


{* Take the first 40 characters *}
Topic: {$topic|truncate:40:"..."}
{* Format text string *}
{"now"|date_format:"%Y/%m/%d"}
{* Apply regulator in custom function *}
{mailto|upper address="main@cn-web.com"}
capitalize(capitalize the first letter)

The index.php page is as follows:

Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');

The index.tpl page is as follows:

Copy code The code is as follows:
{$articleTitle}
{$articleTitle|capitalize}

OUTPUT output is as follows:

Copy code The code is as follows:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.

count_characters (count the number of characters in the variable)

index.php is as follows:

Copy code 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:

Copy code The code is as follows:
{$articleTitle}
{$articleTitle|count_characters}

OUTPUT output is as follows:

Cold Wave Linked to Temperatures.

cat(connection string)
Connect the value in cat to the given variable
index.php is as follows:

Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');

The index.tpl page is as follows:

Copy code The code is as follows:
{$articleTitle|cat:" yesterday."}

OUTPUT output is as follows:

Copy code The code is as follows:
Psychics predict world didn't end yesterday.

count_paragraphs(count number of paragraphs)
Count the number of paragraphs in the variable
index.php is as follows:

Copy code 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:

Copy code The code is as follows:
{$articleTitle}
{$articleTitle|count_paragraphs}

OUTPUT output is as follows:

Copy code 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(count the number of sentences)
Count the number of sentences in the variable
index.php is as follows:

Copy code 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');

index.tpl template is as follows:

Copy code The code is as follows:
{$articleTitle}
{$articleTitle|count_sentences}

OUTPUT output is as follows:

Copy code The code is as follows:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Ax.
2

count_words(count words)
Count the number of words in the variable
index.php is as follows:
Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');

index.tpl template is as follows:

Copy code The code is as follows:
{$articleTitle}
{$articleTitle|count_words}

OUTPUT output is as follows:

Copy code 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 input is empty
Format date and time in the given function serftime();
Timestamps from Unix or mysql (parsable by strtotime) can be passed to smarty.
Designers have full control over date formatting using date_format.
If the data passed to date_format is empty, the second parameter will be used as the time format
index.php is as follows:

Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');

index.tpl:

Copy code 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 output is as follows:

Copy code The 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(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
Set a default value for empty variables.
When the variable is empty or unallocated, the given default value will be output instead.
index.php is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}

OUTPUT output:

Copy code The 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.
Used for html transcoding, url transcoding, converting single quotes on variables without transcoding, hexadecimal transcoding, hexadecimal beautification, or javascript transcoding.
The default is html transcoding
index.php is as follows:

Copy code The 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 code The 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"}

 
OUTPUT输出:
 
复制代码 代码如下:
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%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

indent(缩进)
Parameter Position Type Required Default Description
1 integer No 4 This determines how many characters to indent to.
2 string No (one space) This is the character used to indent with.
在每行缩进字符串,默认是4个字符(pear标准也是).
作为可选参数,你可以指定缩进字符数.
作为第二个可选参数,你可以指定缩进用什么字符代替
index.php如下:
 
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');

 
index.tpl模板:
 
复制代码 代码如下:
{$articleTitle}
 
{$articleTitle|indent}
 
{$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(小写)
将变量字符串小写
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(换行符替换成
)
所有的换行符将被替换成
.同php的nl2br()函数一样.
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 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 regular expression.

2 string Yes n/a This is the string of text to replace with.
What text string to use to replace
index.php is as follows:

Copy code The 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 code The code is as follows:
{* replace each carriage return, tab & new line with a space *}{* Use a space to replace each carriage return carriage, tab, and newline characters*}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}

OUTPUT output:
Copy code The code is as follows:
Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

replace(replace)
Simple search and replace strings
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
The string to be replaced
2 string Yes n/a This is the string of text to replace with.
Text to replace
index.php is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}

OUTPUT output:
Copy code The 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
It is a way to insert spaces or other characters (strings) between each character of a string.
index.php is as follows:
Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}

OUTPUT output:
Copy code The 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 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 format)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
Formatting method used
Is a method of formatting floating point numbers. For example, decimal numbers. Use sprintf syntax to format
index.php is as follows:

Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');

index.tpl template:
Copy code The code is as follows:
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}

OUTPUT output:
Copy code The code is as follows:
23.5787446
23.58
24

strip(remove (excess spaces)
Replace all repeated spaces, newlines and tabs with single ones.
index.php is as follows:

Copy code The code is as follows:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');

index.tpl template:
Copy code The code is as follows:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}

OUTPUT output:
Copy code The code is as follows:
Grandmother of
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 tags between < and >, including < and >.
index.php is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|strip_tags}

OUTPUT output:
Copy code The 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.

truncate(interception)
Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
Specify how many characters to intercept
2 string No ... This is the text to append if truncation occurs.
The string that is intercepted and added after 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
Intercept the beginning of the string. The default is 80.
You can specify the second parameter as what character to add after the intercepted string.
By default, smarty will intercept the end of a word,
If you want to intercept exactly how many characters, change the third parameter to "true"
index.php is as follows:
Copy code The 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 code The 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 output:
Copy code The 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 variables to uppercase
index.php is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|upper}

OUTPUT output:
Copy code The 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 (line width constraint)
You can specify the width of the paragraph (that is, how many characters are in a line, if the number of characters exceeds this number, the line will be broken). The default is 80.
The second parameter is optional and can specify what character to use at the constraint point (the default is the newline character n).
By default smarty will intercept to the end of the word, you can also specify how many characters to intercept exactly
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
Specify the width of the paragraph (sentence)
2 string No n This is the string used to wrap words with.
What character constraints to use
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true).
Whether to constrain exactly to the character
index.php is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"
n"}
{$articleTitle|wordwrap:30:"n":true}

OUTPUT output:
Copy code The 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

from dad she hasn't seen in years.
Blind woman gets new kidney fr
om dad she hasn't seen in year
s.

Combining multiple operators
Operators can be applied to a variable, and they will be applied in combination from left to right. Multiple operators must be separated by the "|" symbol.
The index.php page is as follows:

Copy code The 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 code The code is as follows:
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}

OUTPUT output:

Copy code The 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 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 be helpful to everyone’s PHP programming design.

How to use php variables in smarty templates?

This is the method.

Variable increment problem in smarty template

I tried this:
<{assign var="i" value="1"}> (put outside the loop)
<{assign var="i" value= $i+1}> (increment within the loop)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/901284.htmlTechArticleA summary of commonly used variable operators in Smarty, a summary of smarty operators This article summarizes the commonly used variable operators in Smarty, share it with For your reference. The details are as follows: php template engine smarty...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn