寫 smarty 外掛程式 前奏 動態 註冊 自訂 php 函數 模板中使用

來源:互聯網
上載者:User

 

在使用的smarty的時候,有時候需要用到一些複雜的修飾,但是其本身提供的標籤比較少,

不過其提供了一個註冊的動態註冊的功能,還是挺好用的!

 

$smarty->register_function("date_now", "print_current_date");

 

function print_current_date ($params) {

extract($params);

if(empty($format))

$format="%b %e, %Y";

echo strftime($format,time());

}

 

讓我們看看smarty是怎麼處理的

 

我們定義了一個函數名叫print_current_date,並且把它註冊為date_now。

{date_now format=”%Y/%m/%d”} 

其中format是參數(這個名字決定你在註冊函數中引用的參數的名字)。

在函數中extract($params);是必須的~實參的名字可以任意定義。

其實smarty傳過來的是一個數組,你需要用extract一下.這時在使用這個值。

這樣我們就註冊了一個smarty函數。

那如果傳遞兩個以上的參數呢?

其實也很簡單{date_now arg1=”1″ arg2=”2″ arg3=”3″}

smarty把它們都分裝成數組。

print_r($params);

Array ([arg1]=>1 [arg2]=>2 [arg3]=>3)

 

就是這麼簡單~ just easy just use~ this is 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.