PHP 中使用 Smarty 之四:自訂變數調解器_PHP教程

來源:互聯網
上載者:User
在PHP 中,有很多處理文本的函數,您可以把要處理的文本通過函數處理之後,再調用Smarty 模板引擎中的assign() 賦值給變數,分配到模板中進行顯示。

Smarty 中的變數調解器和PHP 中處理文本的函數相似,不過文法不相同,在Smarty 中,是通過"|" 後面直接跟調解器函數名,如果有參數,得加在":" 後面,多個參數的話,累加即可。

格式:{$var|modifier1:"參數1":"參數2":參數3|modifier2|modifier3|...}

定義調解器的檔案必須放置在Smarty 中,具體路徑是:libs/plugins/。其檔案名稱,必須按照Smarty 的格式modifier.調解器名.php

下面通過一個執行個體示範Smarty 中,自訂變數調解器的使用

程式思路:做兩個變數調解器,功能是:一個轉換文本;一個截取文本。

init.inc.php(Smarty初始設定檔案)

define('ROOT_PATH', dirname(__FILE__)); //設定網站根目錄

require ROOT_PATH.'/libs/Smarty.class.php'; //載入Smarty 模板引擎

$_tpl = new Smarty(); //建立一個執行個體對象

$_tpl->template_dir = ROOT_PATH.'/tpl/'; //設定模板檔案目錄

$_tpl->compile_dir = ROOT_PATH.'./com/'; //設定編譯檔案目錄

$_tpl->left_delimiter = '<{'; //設定左定界符

$_tpl->right_delimiter = '}>'; //設定右定界符

?>

index.php(主檔案)

define('CONST_VAR', 'ABC');

require 'init.inc.php'; //引入模板初始設定檔案

global $_tpl;

$_str = 'abcdEFGHigklmnOPQRSTuvwsYz'; //定義一個字串

$_tpl->assign('str',$_str); //字串賦值給str

$_tpl->assign('str1',strtolower($_str)); //字串全部轉換為小寫賦給str1

$_tpl->assign('str2',strtoupper($_str)); //字串全部轉換為大寫賦給str2

$_tpl->assign('str3',ucfirst($_str)); //字串全部轉換為大寫賦給str3

$_tpl->assign('str4',substr($_str, 0,15).'...');//截取字串前15個字元,後面的用'...'代替,並賦給str4

$_tpl->assign('str5',strtoupper(substr($_str, 0,15)).'...'); //截取字串前15個字元轉換為大寫,後面的用'...'代替,並賦給str4

$_tpl->display('index.tpl'); //引入模板

?>

tpl/index.tpl

Smarty 中的變數調解器

<{$str}>

<{$str1}>

<{$str2}>

<{$str3}>

<{$str4}>

<{$str5}>

<{$str|transform}>

<{$str|transform:"lower"}>

<{$str|transform:"upper"}>

<{$str|transform:"firstdx"}>

<{$str|subString:0:15:"###"}>

<{$str|subString:0:15:"@@@"|transform:"upper"}>

<{$str|transform:"upper"|subString:0:15:"@@@"}>

/libs/plugins/modifier.transform.php(轉換檔調解器)

/**

* smarty_modifier_transform

* 字串轉換的變數調解器函數

* @param string $string 處理字串

* @param string $type 處理類型

*/

function smarty_modifier_transform($string,$type) {

switch ($type) {

case 'upper' :

$str = strtoupper($string);

break;

case 'lower' :

$str = strtolower($string);

break;

case 'firstdx' :

$str = ucfirst($string);

break;

default:

$str = $string;

}

return $str;

}

?>

lib/plugins/modifier.subString.php(截取文本調解器)

/**

* smarty_modifier_subString

* 處理截取字串調解器

* @param string $string 處理字串

* @param int $start_num 開始位置,預設從頭開始

* @param int $end_num 結束位置,預設20

* @param string $addTo 追加字串,預設'...'

*/

function smarty_modifier_subString($string,$start_num=0,$end_num=20,$addTo='...') {

$_str = '';

if (strlen(substr($string, $start_num, $end_num))>=$end_num) {

$_str = substr($string, $start_num, $end_num).$addTo;

} else {

$_str = substr($string, $start_num, $end_num);

}

return $_str;

}

?>

執行結果:

通過上面的執行個體,表明調解器檔案必須放在Smarty 的外掛程式目錄plugins 下,並且命名必須遵循Smarty 的規則,這樣,才能調用到您編寫的調解器函數。還有一點需要說明,定義的函數名稱也必須符合Smarty 內定的命名規則,例如:smarty_modifier_XXX,並且一個調解器檔案,只能放一個函數,不能放置多個。

好了,自訂調解器先介紹到這裡,Smarty 中有很多已經寫的調解器函數,可以拿來直接調用或修改成您自己喜歡的風格。關於Smary 內建的調解器,下節介紹


摘自:Lee.的專欄

http://www.bkjia.com/PHPjc/478570.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478570.htmlTechArticle在PHP 中,有很多處理文本的函數,您可以把要處理的文本通過函數處理之後,再調用Smarty 模板引擎中的assign() 賦值給變數,分配到模板中...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.