PHP模板引擎Smarty自訂變數調解器用法,模板smarty_PHP教程

來源:互聯網
上載者:User

PHP模板引擎Smarty自訂變數調解器用法,模板smarty


本文執行個體講述了PHP模板引擎Smarty自訂變數調解器用法。分享給大家供大家參考,具體如下:

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

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

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

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

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

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

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

<?php  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(主檔案)

<?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(轉換檔調解器)

<?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(截取文本調解器)

<?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 內建的調解器,後續章節會有詳細介紹。

更多關於PHP相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於smarty模板的PHP程式設計有所協助。

您可能感興趣的文章:

  • PHP模板引擎Smarty內建函數詳解
  • PHP模板引擎Smarty內建變數調解器用法詳解
  • PHP模板引擎Smarty中的保留變數用法分析
  • PHP模板引擎Smarty內建函數foreach,foreachelse用法分析
  • PHP模板引擎Smarty之設定檔在模板變數中的使用方法樣本
  • PHP模板引擎Smarty中變數的使用方法樣本
  • smarty模板引擎從php中擷取資料的方法
  • ThinkPHP使用smarty模板引擎的方法
  • 在PHP模板引擎smarty產生隨機數的方法和math函數詳解
  • PHP模板引擎Smarty的緩衝使用總結
  • php smarty模板引擎的6個小技巧
  • [PHP]模板引擎Smarty深入淺出介紹
  • PHP模板引擎Smarty內建函數section,sectionelse用法詳解

http://www.bkjia.com/PHPjc/1119973.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1119973.htmlTechArticlePHP模板引擎Smarty自訂變數調解器用法,模板smarty 本文執行個體講述了PHP模板引擎Smarty自訂變數調解器用法。分享給大家供大家參考,具體如...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.