Smarty(變量修飾器)

1、概念

變量修飾器(調節器)可用於變量,自定義函數和字符串。 請使用‘|’符號和修飾器名稱應用修飾器。 變量修飾器由賦予的參數值決定其行爲。 參數由‘’符號分開。javascript

2、修飾器用法簡介

capitalize

使變量內容裏的每一個單詞的第一個字母大寫。 與PHP函數的 ucwords( )類似。php


參數1:帶數字的單詞是否也頭字母大寫(Boolean,默認:false)
參數2:設置單詞內其餘字母是否小寫,如"aA" 變成 "Aa"(Boolean,默認:false)html


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'I want to buy a samsung s8');
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|capitalize}
{$articleTitle|capitalize:true}

OUTPUT:

I want to buy a samsung s8
I Want To Buy A Samsung s8
I Want To Buy A Samsung S8

注:lower修飾器將變量值轉成小寫字母(全部字母),無參數。
upper與之相反將變量值轉成大寫字母(全部字母),無參數。java

cat

鏈接多個變量。mysql


參數1:須要鏈接的變量(String)。正則表達式


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "To be or not to be");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|cat:'it is a question'}

OUTPUT:

To be or not to be
To be or not to be it is a question

count_characters

計算變量內容裏有多少個字符。sql


參數1:計算總數時是否包括空格字符(Boolean,默認:false)。api


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "Cold Wave Linked to Temperatures");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_characters}
{$articleTitle|count_characters:true}

OUTPUT:

Cold Wave Linked to Temperatures.
29
33

count_paragraphs

計算變量內容有多少個段落。less


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy!\n\n
                                Yes I am!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_paragraphs}

OUTPUT:

I am a good guy!
Yes I am!
2

count_sentences

計算變量內容有多少個句子。 每一個句子必須以點號、問號或者感嘆號結尾。
(./?/!)函數


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am! Are you sure? Yes!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_sentences}

OUTPUT:

I am a good guy.Yes I am! Are you sure? Yes!
4

count_words

用於計算變量內容有多少個單詞。


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am! Are you sure? Yes!");
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|count_words}

OUTPUT:

I am a good guy.Yes I am! Are you sure? Yes!
12

date_format

將日期和時間格式化成strftime( )的格式。 時間能夠是unix的 時間戳, DateTime 對象, mysql時間戳,或者月日年格式的字符串,與PHP函數strtotime( )相似。而且能夠對date_format的格式有徹底的控制。 若是傳遞到date_format的時間爲空, 而第二個參數傳遞了值,那麼第二個參數將做爲須要格式化的時間。


參數1:輸出時間的格式定義(String,默認:%b %e, %Y)
參數2:若是輸入爲空,則做爲默認時間(String,默認:n/a)


index.php:

$smarty = new Smarty;
$config['date'] = '%I:%M %p';
$config['time'] = '%H:%M:%S';
$smarty->assign('config', $config);
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');

index.tpl:

{$smarty.now|date_format}
{$smarty.now|date_format:"%D"}
{$smarty.now|date_format:$config.date}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:$config.time}

OUTPUT:

Jan 1, 2022
01/01/22
02:33 pm
Dec 31, 2021
Monday, December 1, 2021
14:33:00

附:date_format支持格式
W3C DATE_FORMAT( ) 函數

default

爲變量設置默認值。 當變量是unset或者empty的字符串時,默認值將顯示。 必需要有一個參數。


參數1:當變量爲空時輸出的值(String)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.Yes I am!");
$smarty->assign('email', '');
$smarty->display('index.tpl');

index.tpl:

{$articleTitle|default:'no title'}
{$myTitle|default:'no title'}
{$email|default:'No email address available'}


OUTPUT:

I am a good guy.Yes I am!
no title
No email address available

escape

escape可用於將變量編碼或轉換成 html, url, 單引號, 十六進制, 十六進制實體, javascript 和 email地址。 默認是:html。


參數1:這是escape轉換後的格式(String,默認:html,可取值:html, htmlall, url, urlpathinfo, 單引號, 十六進制, 十六進制實體, javascript, email地址)
參數2:傳遞給htmlentities( )的字符集類型(String,默認:UTF-8,可取值:ISO-8859-1, UTF-8, 和其餘 htmlentities()支持的字符集)
參數3:兩次轉換實體,& 到 & (僅在 html 和 htmlall 使用)(Boolean,默認:true)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle',
                "'Stiff Opposition Expected to Casketless Funeral Plan'"
                );
$smarty->assign('EmailAddress','smarty@example.com');

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

index.tpl & OUTPUT:

{$articleTitle}
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape}
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape:'html'}  
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape:'htmlall'} 
'Stiff Opposition Expected to Casketless Funeral Plan'

<a href="?title={$articleTitle|escape:'url'}">click here</a>
<a href="?title=%27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27">click here</a>

{$articleTitle|escape:'quotes'}
\'Stiff Opposition Expected to Casketless Funeral Plan\'

<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'}    {* this converts to email to text *}
<a href="mailto:%62%6f%..snip..%65%74">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>

{'mail@example.com'|escape:'mail'}
smarty [AT] example [DOT] com

注:unescape能夠解碼entity, html 和 htmlall等的編碼。 它與escape 修飾器的效果恰好相反。

indent

縮進每一行的字符串,默認是縮進4個空格。 可選的參數能夠設置縮進的空格數量。 可選的第二個參數設置縮進使用的字符,如用 "t" 來代替空格縮進。


參數1:設置縮進多少空格(Integer,默認:4)
參數2:設置用什麼字符來進行縮進(String,默認:一個空格)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle',
                'NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.'");
$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.

nl2br

將變量值中的"n"回車所有轉換成HTML的<br/>。 等同於PHP的 nl2br()函數。

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.\nYes I am!")
$smarty->display('index.tpl');

index.tpl:

{$articleTitle|nl2br}

OUTPUT:

I am a good guy.<br/>Yes I am!

regex_replace

用正則表達式搜索和替換變量值。 使用PHP的 preg_replace()函數進行。


參數1:正則表達式(String,默認:n/a)
參數2:替換的字符(String,默認:n/a)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', "I am a good guy.\nYes I am!")
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "}

OUTPUT:

I am a good guy.
Yes I am!
I am a good guy. Yes I am!

注:replace修飾器用法類似,對變量進行簡單的搜索和替換。 等同於PHP函數的 str_replace()。

spacify

spacify會在變量的字符串中插入空格。 你能夠設置插入的是空格或者別的字符。


參數1:插入字符間的字符(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 .... snip ....  s h ,   E x p e r t s   S a y .
S^o^m^e^t^h^i^n^g^.... snip .... ^e^r^t^s^ ^S^a^y^.

string_format

格式化字符串,如浮點數等。 使用 sprintf()的PHP函數來進行。


參數1:指定哪一種格式 (sprintf)(String,默認:n/a)


index.php:

$smarty = new Smarty;
$smarty->assign('number', 3.1415926);
$smarty->display('index.tpl');

index.tpl:

{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}


OUTPUT:

3.1415926
3.14
3

strip

轉換連續空格,回車和tab到單個空格或是指定字符串。
PS.若是但願轉換模板文字內的空格,使用內置的 {strip} 函數。

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 「I am so\ngood that\t    they all like me");
$smarty->display('index.tpl');

index.tpl:

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

OUTPUT:

I am so
good that        they all like me
I am so good that they all like me 
I&nbsp;am&nbsp;so&nbsp;good&nbsp;that&nbsp;they&nbsp;all&nbsp;like&nbsp;me

truncate

截取字符串到指定長度,默認長度是80.


參數1:截取的長度(Integer,默認:80)
參數2:截取後替代顯示的字符,該字符長度會被計算到截取長度內(String,默認:…)
參數3:默認truncate會嘗試按單詞進行截取。 若是你但願按字符截取(單詞可能會被截斷),須要設置第三個參數TRUE。(Boolean,默認:false)


index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two 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}
{$articleTitle|truncate:30:'..':true:true}

OUTPUT:

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...
Two Sisters Re..ckout Counter

wordwrap

限制一行字符的長度(自動換行),效果與PHP函數wordwrap()同樣。


參數1:限定一行的長度(Integer,默認:80)
參數2:換行符號,可自定義換行字符(String,默認:n)
參數3:設置按單詞換行(FALSE),或者按字符換行(TRUE),默認狀況下,是根據單詞來換行的,也就是按英文語法的自動換行。 若是你但願按照字符來換行(邊界的單詞將拆開),那麼能夠設置 可選的第三個參數爲TRUE


index.php:

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

$articleTitle}

{$articleTitle|wordwrap:30}

{$articleTitle|wordwrap:20}

{$articleTitle|wordwrap:30:"<br />\n"}

{$articleTitle|wordwrap:26:"\n":true}

OUTPUT:

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<br />
years

Blind woman gets new kidn
ey from dad she hasn't se
en in years

3、複合修飾器

能夠聯合使用多個修飾器。 它們會按複合的順序來做用於變量,從左到右。 它們必須以「|」 (豎線)進行分隔。

相關文章
相關標籤/搜索