首頁 php教程 PHP开发 PHP模板引擎Smarty內建變數調解器用法詳解

PHP模板引擎Smarty內建變數調解器用法詳解

Dec 27, 2016 pm 04:00 PM

本文實例講述了PHP模板引擎Smarty內建變數調解器用法。分享給大家供大家參考,具體如下:

Smarty 中的變數調解器相當於函數,其呼叫方式為:透過"|" 後面直接跟調解函數名,如果有參數,得加在":" 後面,多個參數的話,累加即可。

下面為您介紹 Smarty 中內建的變數調解器:

1、capitalize

將變數裡的所有單字首字大寫。參數值 boolean 型決定帶數字的單字,首字是否大寫。預設不大寫

index.php

$tpl->assign('str', 'hello world wor2ld!!!');
$tpl->display('index.html');
登入後複製

index.html(範本檔案)

<{$str|capitalize}>
<{$str|capitalize:true}>
登入後複製

結果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!

2、count_characters

,此調解器預設不計算空格(空格、製表符、回車…)只計算字元的個數,並且能很好的支援中文字元計算;如果新增參數true ,則計算空格。

index.html

<{$str|count_characters}> // 不计算空格
<{$str|count_characters:true}> // 计算空格
登入後複製

結果為:13、14

3、cat

連接字串,將cat裡的值連接到給定的變數後面。

<{$str|cat:&#39; Happy new year.&#39;}>
登入後複製

   

結果為:hello world!!! Happy new year.

4、count_paragraphs

計算段數,計算變數裡的段落數量,完美支持中文。

index.php

$str = <<assign(&#39;str&#39;, $str);
$tpl->display(&#39;index.html&#39;);
登入後複製

   

index.html

<{$str|count_paragraphs}>
登入後複製

   

ten,註:只支援英文語句,不支援中文。

index.php

$str = <<assign(&#39;str&#39;, $str);
登入後複製
登入後複製

   

index.html

   
<{$str|count_sentences}>
登入後複製

   

字結果數為:23553字數的數字計算、術語數。

index.php

$str = <<assign(&#39;str&#39;, $str);
登入後複製
登入後複製

   

index.html

<{$str|count_words}>
登入後複製

   

index.php

$tpl->assign(&#39;date&#39;, time()); // 传递时间戳
登入後複製

   

index.html

<{$date|date_format:&#39;%Y-%m-%d %H:%M:%S&#39;}>
登入後複製

   

結果為:2012-01-26 14:37:22

8、default

默認,為空變量設置一個默認值,當變數為空或未分配的時候,將由給定的預設值替代輸出。

index.php

$tpl->assign(&#39;str&#39;, &#39;&#39;); // 赋值给空
登入後複製

   

index.html

<{$str|default:&#39;默认输出...&#39;}>、<{$string|default:&#39;没有定义,默认输出...&#39;}>
登入後複製

   

.於html 轉碼,url 轉碼,在沒有轉碼的變數上轉換單引號,十六進位轉碼,十六進位美化,或javascript 轉碼,預設是html轉碼

index.php

$html = <<Google
html;
$js = <<
  for (var i=0; i<100; i++) {
    window.alert(i);
  }
js;
$tpl->assign(&#39;html&#39;, $html); // html
$tpl->assign(&#39;url&#39;, &#39;http://www.google.com.hk&#39;); // url
$tpl->assign(&#39;js&#39;, $js); // javascript
登入後複製

   

index.html

HTML 转码:<{$html|escape:"html"}>
URL 转码:<{$url|escape:"url"}>
JS 转码:<{$js|escape:"javascript"}>
登入後複製

   

結果為:

HTML 转码:Google
URL 转码:http%3A%2F%2Fwww.google.com.hk
JS 转码:
登入後複製

進多少個字符串,預設是四個字元;第二個參數,指定縮排用什麼字元取代。

11、lower

小寫,將變數字串小寫。

使用方法:<{$str|lower}>

12、upper

大寫,將變數改為大寫。

使用方法:<{$str|upper}>

13、nl2br

換行符替換成

所有的換行符將被替換成 ,同php的nl2br()函數一樣。

14、regex_replace

正規替換,尋找和替換正規表示式,和 preg_replace() 的語法一樣。

index.php

$tpl->assign(&#39;str&#39;, &#39;http://www.google.com&#39;);
登入後複製

   

index.html

<{$str|regex_replace:&#39;/go{2}gle/&#39;:&#39;baidu&#39;}>
登入後複製

   

16、spacify

插空,插空(不知道這個字是什麼意思,顧名思義了^^)是一種在字串的每個字元之間插入空格或其他的字元(字串)。

index.php

$tpl->assign(&#39;str&#39;, &#39;hello world!!!&#39;);
登入後複製

   

index.html

<{$str|spacify:"^^"}>
登入後複製

   

^^

^^^^ ^l^^d^^!^^!^^!

17、string_format

字串格式化,是一種格式化浮點數的方法,例如:十進制數.使用sprintf 語法格式化。

index.php

$tpl->assign(&#39;num&#39;, 23.5787446);
登入後複製

   

index.html

<{$num|string_format:"%.2f"}>
<{$num|string_format:"%d"}>
登入後複製

   

、23.58、23383.單一

index. php

$tpl->assign(&#39;str&#39;, "Grandmother of\neight makes\t  hole in one.");
登入後複製

   

index.html

<{$str|strip:" "}>
登入後複製

   

結果為:Grandmother of eightkes makes

 hole in one.

19、strip_tags

去除在<和>之間的所有標籤,包括<和>。

index.php

$tpl->assign(&#39;str&#39;, "Google");
登入後複製

   

index.html

<{$str|strip_tags}>
登入後複製

   

.

截取,截取字符串开始的一段.默认是80个,你可以指定第二个参数作为在截取的那段字符串后加上什么字符,默认情况下,smarty会截取到一个词的末尾,如果你想要精确的截取多少个字符,把第三个参数改为"true" 。

index.php

$tpl->assign(&#39;str&#39;, &#39;从前有座山,山上有座庙。庙里有一个老和尚和一个小和尚...&#39;);
登入後複製

index.html

<{$str|truncate:10:&#39;...&#39;:true}>
登入後複製

   

结果为:从前有座山,山...

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

更多PHP模板引擎Smarty内置变量调解器用法详解相关文章请关注PHP中文网!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)