Commonly used smarty variable-character operations

Source: Internet
Author: User

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]
Replace the nl2br[newline with <br/>]
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
Instance:
{* Title Uppercase *}

{* 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= "[Email protected]"}
Capitalize (first letter capital)
index.php:

$smarty = new Smarty;
$smarty->assign (' ArticleTitle ', ' Police begin campaign to rundown jaywalkers. ');
$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}
{$articleTitle |capitalize}

OUTPUT:

Police begin campaign to rundown jaywalkers.
Police Begin Campaign to Rundown jaywalkers.


Count_characters (number of characters in the calculated 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.

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

$smarty = new Smarty;
$smarty->assign (' ArticleTitle ', ' psychics predict world didn ' t end ');
$smarty->display (' Index.tpl ');

Index.tpl:

{$articleTitle |cat: "Yesterday."}

OUTPUT:

Psychics predict world didn ' t end yesterday.

count_paragraphs (Count 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.
2

count_sentences (Count of Sentences)
calculates the number of sentences in a variable
index.php:

$smarty = new Smarty;
$smarty->assign (' ArticleTitle ', ' both Soviet Ships Collide-one dies. Enraged Cow injures Farmer with Axe. ');
$smarty->display (' Index.tpl ');

Index.tpl:

{$articleTitle}
{$articleTitle |count_sentences}

OUTPUT:

Both Soviet Ships Collide-one dies. Enraged Cow injures Farmer with Axe.
2
Count_words (count of words)
count the number of words in a variable
index.php:

$smarty->assign (' articletitle ', ' dealers'll hear Car talk at Noon. ');
$smarty->display (' Index.tpl ');

Index.tpl:

{$articleTitle}
{$articleTitle |count_words}

OUTPUT:

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 is the format for the outputted date.
The 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:

$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 (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:

$smarty = new Smarty;
$smarty->assign (' articletitle ', ' dealers'll hear Car talk at Noon. ');
$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle |default: "No title"}
{$myTitle |default: "No title"}

OUTPUT:

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:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', "' Stiff opposition expected to casketless funeral Plan ');

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |escape}

{$articleTitle |escape: "HTML"} {* Escapes & ' < > *}

{$articleTitle |escape: "Htmlall"} {* Escapes all HTML entities *}

{$articleTitle |escape: "url"}

{$articleTitle |escape: "Quotes"}

<a

href= "{$EmailAddress |escape:" hexentity "}mailto:{$EmailAddress |escape:" Hex "}" >{$EmailAddress |escape: " Hexentity "}</a>

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 '

<a

href= "[Email protected]:%62%6f%62%40%6d%65%2e%6e%65%74" >& #x62;& #x6f;& #x62;& #x40;& #x6d; & #x65;& #x2e;& #x6e;& #x65;& #x74;</a>

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:

$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 (lowercase)
Lowercase the variable string
index.php:

$smarty = new Smarty;

$smarty->assign (' articletitle ', ' convicts Evade noose, jury Hung. ');

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |lower}

OUTPUT:

Convicts Evade noose, jury Hung.

Convicts evade noose, jury hung.


NL2BR (line break replaced with <br/>)
All newline characters will be replaced with <br/>. Same as PHP's NL2BR () function.
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<br/>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:

$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, tab, and line break with a space *}

{$articleTitle}

{$articleTitle |regex_replace: "/[rtn]/": "}

OUTPUT:

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:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', "child ' s stool great for use in the garden.");

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |replace: "Garden": "Vineyard"}

{$articleTitle |replace: "": "}

OUTPUT:

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:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', ' Something Went wrong in Jet Crash, experts Say. ');

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |spacify}

{$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 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:

$smarty = new Smarty;

$smarty->assign (' number ', 23.5787446);

$smarty->display (' Index.tpl ');

INDEX.TPL:

{}

{|string_format: "%.2f"}

{|string_format: "%d"}

OUTPUT:

23.5787446

23.58

24


Strip (remove (extra spaces)
Replace all repeating spaces, line breaks, and tab to individual.
index.php:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', "grandmother Ofneight makest Hole in one.");

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |strip}

{$articleTitle |strip: "&nbsp;"}

OUTPUT:

Grandmother of

Eight makes hole in one.

Grandmother of eight makes hole in one.

Grandmother&nbsp;of&nbsp;eight&nbsp;makes&nbsp;hole&nbsp;in&nbsp;one.


Strip_tags (remove HTML tags)
Remove all labels between < and >, including < and;.
index.php:

$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:

{$articleTitle}

{$articleTitle |strip_tags}

OUTPUT:

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 (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:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', ' both sisters reunite after eighteen years at Checkout Counter. ');

$smarty->display (' Index.tpl ');

INDEX.TPL:

{$articleTitle}

{$articleTitle |truncate}

{$articleTitle |truncate:30}

{$articleTitle |truncate:30: ""}

{$articleTitle |truncate:30: "---"}

{$articleTitle |truncate:30: "": true}

{$articleTitle |truncate:30: "...": true}

OUTPUT:

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:

$smarty = new Smarty;

$smarty->assign (' ArticleTitle ', "If Strike isn ' t settled Quickly it ' 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 (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:
$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:
{$articleTitle}
{$articleTitle |wordwrap:30}
{$articleTitle |wordwrap:20}
{$ ARTICLETITLE|WORDWRAP:30: "<br>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 the 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.

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.
index.php:

$smarty = new Smarty;
$smarty->assign (' articletitle ', ' smokers is 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: "...}

Commonly used smarty variable-character operations

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.