Summary of common variable operators in smarty, smarty operator Summary _php Tutorial

Source: Internet
Author: User
Tags php programming php template sprintf smarty template truncated

Summary of common variable operators in smarty, smarty operator Summary


This article summarizes the common variable operators in Smarty and shares them for everyone's reference. Specific as follows:

The PHP template engine smarty variable operators can be used to manipulate variables, custom functions and characters.
Use "|" in Syntax Apply variable operator, multiple parameters with ":"?? Referring to Dian?/div>

capitalize[Initial capital Letter]
count_characters[count of characters]
cat[Connection string]
count_paragraphs[count of paragraphs]
count_sentences[Calculation of sentences]
count_words[calculation of words]
date_format[Time format]
default[Default]
escape[transcoding]
indent[Indent]
lower[Lowercase]
nl2br[newline character replaced by
]
regex_replace[Regular Replacement]
replace[Replace]
spacify[intervening spaces]
string_format[string Formatting]
strip[removal (extra spaces)]
strip_tags[Remove HTML tags]
Truncate[Intercept]
Upper[Caps]
wordwrap[line width constraint]
Using multiple operators in combination

Examples are as follows:

Copy the Code Code as follows: {* Title uppercase *}

{$title |upper}


{* Take its first 40 characters *}
Topic: {$topic |truncate:40: "..."}
{* Formatted text string *}
{"Now" |date_format: "%y/%m/%d"}
{* Apply adjuster in custom function *}
{mailto|upper address= "main@cn-web.com"}
Capitalize (first letter capital)

The index.php page is as follows:

Copy the Code code 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 the Code Code as follows: {$articleTitle}
{$articleTitle |capitalize}

The output outputs are as follows:

Copy the Code code as follows: Police begin campaign to rundown jaywalkers.
Police Begin Campaign to Rundown jaywalkers.

Count_characters (number of characters in the calculated variable)

index.php as follows:

Copy the Code code 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 the Code Code as follows: {$articleTitle}
{$articleTitle |count_characters}

The output outputs are as follows:

Cold Wave Linked to temperatures.

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

Copy the Code code 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 the code as follows: {$articleTitle |cat: "Yesterday."}

The output outputs are as follows:

Copy the Code Code as follows: Psychics predict world didn ' t end yesterday.

Count_paragraphs (count of segments)
Calculate the number of paragraphs in a variable
index.php as follows:

Copy the Code code 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 the Code Code as follows: {$articleTitle}
{$articleTitle |count_paragraphs}

The output outputs are as follows:

Copy the Code code 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 of Sentences)
Calculate the number of sentences in a variable
index.php as follows:

Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', ' Soviet Ships Collide-one dies. Enraged Cow injures Farmer with Axe. ');
$smarty->display (' Index.tpl ');

The INDEX.TPL template is as follows:

Copy the Code Code as follows: {$articleTitle}
{$articleTitle |count_sentences}

The output outputs are as follows:

Copy the Code code as follows: Soviet Ships Collide-one dies. Enraged Cow injures Farmer with Axe.
2
Count_words (count of words)
Calculate the number of words in a variable
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' articletitle ', ' dealers'll hear Car talk at Noon. ');
$smarty->display (' Index.tpl ');

The INDEX.TPL template is as follows:

Copy the Code Code as follows: {$articleTitle}
{$articleTitle |count_words}

The output outputs are as follows:

Copy the Code code as follows: Dealers'll hear Car talk at Noon.
7
Date_format (date format)
Parameter Position
Parameter position Type Required Default Description
1 string No%b%e,%Y This was the format for the outputted date.
Format of the output string
2 string No N/A This is the default date if the-input is empty.
Default setting when input is empty
In the given function serftime (), the format date and time.
Timestamps (parsable by Strtotime), such as UNIX or MySQL, can be passed to Smarty.
Designers can use Date_format to fully control the date format.
If the data passed to Date_format is empty, the second parameter will be used as the time format
index.php as follows:

Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' Yesterday ', Strtotime ('-1 day '));
$smarty->display (' Index.tpl ');

INDEX.TPL:

Copy the Code Code 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"}

The output outputs are as follows:

Copy the Code code as follows: Feb 6, 2001
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
Default (Defaults)
Parameter Position Type Required Default Description
1 string No Empty 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 the null variable.
When the variable is empty or unassigned, the output is replaced by the given default value.
index.php as follows:

Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' articletitle ', ' dealers'll hear Car talk at Noon. ');
$smarty->display (' Index.tpl ');

INDEX.TPL templates:

Copy the code as follows: {$articleTitle |default: "No title"}
{$myTitle |default: "No title"}

Output outputs:

Copy CodeThe code is as follows: Dealers'll hear Car talk at Noon.
No title
Escape (Transcode)
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.
For HTML transcoding, URL transcoding, converting single quotes, hexadecimal transcoding, hex beautification, or JavaScript transcoding on non-transcoding variables.
The default is HTML transcoding
index.php 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 templates:

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 "}

Output outputs:

Copy CodeThe code is as follows: ' 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 (indent)
Parameter Position Type Required Default Description
1 integer No 4 This determines how many characters to indent.
2 string No (one space) This is the character used to indent with.
The string is indented in each line, and the default is 4 characters (Pear standard is also).
As an optional parameter, you can specify the number of indent characters.
As the Second optional parameter, you can specify what character the indentation will use instead of
index.php 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 templates:

Copy CodeThe code is as follows: {$articleTitle}

{$articleTitle |indent}

{$articleTitle |indent:10}

{$articleTitle |indent:1: "T"}

Output outputs:

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 (lowercase)
Lowercase the variable string
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' articletitle ', ' convicts Evade noose, jury Hung. ');
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the Code Code as follows: {$articleTitle}
{$articleTitle |lower}
Output outputs:
Copy the Code Code as follows: Convicts Evade noose, jury Hung.
Convicts evade noose, jury hung.

NL2BR (line break replaced by
)
All newline characters will be replaced with
. Same as PHP's NL2BR () function.
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "Sun or Rain expectedntoday, Dark Tonight");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the Code Code as follows: {$articleTitle |NL2BR}
Output outputs:
Copy the Code code as follows: 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 being replaced.
Replace the 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 as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "infertility unlikely tonbe passed on, experts say.");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the code as follows: {* Replace each carriage return, tab & new lines with a space *}{* use spaces to replace each carriage return, tab, and newline character *}
{$articleTitle}
{$articleTitle |regex_replace: "/[rtn]/": "}
Output outputs:
Copy the Code code as follows: Infertility unlikely to
Be passed on, experts say.
Infertility unlikely to being passed on, experts say.

Replace (replacement)
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.
The text to replace
index.php as follows:
Copy CodeThe code is as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "child ' s stool great for use in the garden.");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy CodeThe code is as follows: {$articleTitle}
{$articleTitle |replace: "Garden": "Vineyard"}
{$articleTitle |replace: "": "}
Output outputs:
Copy CodeThe code is as follows: Child's stool great for use in the garden.
Child's stool great for use in vineyard.
Child's stool great for use in the garden.
Spacify
is a way to insert a space between each character in a string or to insert a different character (string).
index.php 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 templates:
Copy CodeThe code is as follows: {$articleTitle}
{$articleTitle |spacify}
{$articleTitle |spacify: "^^"}
Output outputs:
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 the what format to use. (sprintf)
How to use formatting
is a method for formatting floating-point numbers. For example, decimal number. Formatting using sprintf syntax
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' number ', 23.5787446);
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the Code Code as follows: {$number}
{$number |string_format: "%.2f"}
{$number |string_format: "%d"}
Output outputs:
Copy the Code code as follows: 23.5787446
23.58
24

Strip (remove (extra spaces)
Replace all repeating spaces, line breaks, and tab to individual.
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "grandmother Ofneight makest Hole in one.");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the Code Code as follows: {$articleTitle}
{$articleTitle |strip}
{$articleTitle |strip: ""}
Output outputs:
Copy the Code code 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)
Removal in <和> All the labels between, including <和> .
index.php 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 templates:
Copy CodeThe code is as follows: {$articleTitle}
{$articleTitle |strip_tags}
Output outputs:
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.
Truncate (intercept)
Parameter Position Type Required Default Description
1 integer No determines how many characters to truncate.
Specify how many characters to intercept
2 String No ... The text to append if truncation occurs.
A string that is added after the Intercept word.
3 Boolean No False this determines whether or not to truncate at a word boundary (false), or at the exact character (true) .
Check to see if the boundary of the word is intercepted
Intercepts the beginning of a string. The default is 80.
You can specify the second argument as what character to add after the truncated string.
By default, Smarty is truncated to the end of a word,
If you want to accurately intercept the number of characters, change the third parameter to "true"
index.php as follows:
Copy CodeThe code is as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', ' both sisters reunite after eighteen years at Checkout Counter. ');
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
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 outputs:
Copy CodeThe code is as follows: Sisters reunite after eighteen years at Checkout Counter.
Sisters reunite after eighteen years at Checkout Counter.
Sisters reunite after ...
Sisters reunite after
Sisters reunite after---
Sisters reunite after Eigh
Sisters reunite after E ...

Upper (uppercase)
Change a variable to uppercase
index.php as follows:
Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "If Strike isn ' t settled Quickly it ' a while.");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy the Code Code as follows: {$articleTitle}
{$articleTitle |upper}
Output outputs:
Copy the code 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 row, which is more than the number of characters wrapped). Default 80.
The second parameter is optional, and you can specify what character to use at the constraint point (the default is newline character N).
By default Smarty will intercept the ending, and you can specify exactly how many characters to intercept
Parameter Position Type Required Default Description
1 integer No determines how many columns to wrap to.
Specify the width of the paragraph (sentence)
2 string No n This was the string used to wrap words with.
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).
is the exact constraint to the character
index.php as follows:
Copy CodeThe code is as follows: $smarty = new Smarty;
$smarty->assign (' ArticleTitle ', "Blind woman gets new kidney from the dad she hasn ' t seen in years.");
$smarty->display (' Index.tpl ');
INDEX.TPL templates:
Copy CodeThe code is as follows: {$articleTitle}
{$articleTitle |wordwrap:30}
{$articleTitle |wordwrap:20}
{$articleTitle |wordwrap:30: "
N "}
{$articleTitle |wordwrap:30: "n": true}
Output outputs:
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

From the dad she hasn ' t seen in years.
Blind woman gets New kidney fr
Om dad she hasn ' t seen in year
S.
Using multiple operators in combination
Operators can be applied on a variable, and they will be applied from left to right. Multiple operators must use the ' | ' Symbols separately.
The index.php page is as follows:

Copy the Code code as follows: $smarty = new Smarty;
$smarty->assign (' articletitle ', ' smokers is productive, but death Cuts efficiency. ');
$smarty->display (' Index.tpl ');

INDEX.TPL templates:
Copy the Code Code as follows: {$articleTitle}
{$articleTitle |upper|spacify}
{$articleTitle |lower|spacify|truncate}
{$articleTitle |lower|truncate:30|spacify}
{$articleTitle |lower|spacify|truncate:30: "..."}

Output outputs:

Copy the Code Code as follows: Smokers is 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.
smokersareproductive,butdeathcuts...
smokersareproductive,but...
S M o K e R s a r e p ...

I hope this article is helpful to everyone's PHP programming.


How do I use PHP variables in a smarty template?

That's the way it is.

Variable self-increment problem in smarty template

Try it. This can be:
<{assign var= "I" value= "1"}> (placed outside the loop)
<{assign var= "i" value= $i +1}> (increment within the loop)

http://www.bkjia.com/PHPjc/901284.html www.bkjia.com true http://www.bkjia.com/PHPjc/901284.html techarticle smarty Common variable operator summary, Smarty operator Summary This article summarizes the smarty commonly used variable operators, share to everyone for your reference. Specific as follows: PHP template engine smarty ...

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