php學習筆記(三十六)smarty中使用自定函數和代碼塊(smarty2和smarty3的不同)____函數

來源:互聯網
上載者:User



初始化

init.smarty.php

<?phpdefine("ROOT", ".");//解決問題:Warning: strftime() [function.strftime]:date_default_timezone_set("Asia/Shanghai");include ROOT."/libs/Smarty.class.php";$tpl = new Smarty();//smarty初始化$tpl->template_dir=ROOT."/templates/";$tpl->compile_dir=ROOT."/templates_c/";//設定檔位置$tpl->config_dir=ROOT."/configs/";$tpl->left_delimiter="<!--{";$tpl->right_delimiter="}-->";?>

一:在php檔案中定義函數和塊的使用

mysmarty.php

<?php/** *  * 以下所有使用變數都要基於設定的首碼和尾碼<!--{}--> * 函數使用 *  * 1.在Smarty中是以自訂標籤(類似於html標記)的使用形式,來使用函數 *  * 2.使用smarty函數 * 2.1單行標記 * php中 * //smarty2的方式 * $tpl->register_function("hello","say"); * //smarty3的方式 * $tpl->registerPlugin("function","hello","say"); * 模板中調用 * <!--{hello size=5 color="green" content="22222" num=5 }--> * 2.2塊標記 * php中 * //smarty2的方式 * $tpl->register_block("world","test"); * //smarty3的方式 * $tpl->registerPlugin("block","world","test"); * 模板中調用 * <!--{world size=8 color="red" num=4 }--> * world<!--{$title}--><br> * <!--{/world}--> * 3.傳值 * php * $tpl->assign("title",$title); * $tpl->assign("color","pink"); * $tpl->assign("content","content"); * 模板(如果是關聯陣列需要使用1旁邊的反引號來·來講關聯變數包含來進行先解析) * 可以在頁面中使用$color和$content * <!--{hello size=5 color=$color content="$content" num=5 }--> * <!--{hello size=5 color=$color content="`$arr.one`" num=5 }--> *  *  *///如果檔案載入失敗require會停止繼續解析php;而include則會繼續向下執行require 'init.smarty.php';$title="這是一個文字標題";$tpl->assign("title",$title);$tpl->assign("color","pink");$tpl->assign("content","content");$tpl->assign("arr",array("one"=>"arr"));//註冊單行函數//smarty2的方式//$tpl->register_function("hello","say");//$tpl->register_block("world","test");//smarty3的方式$tpl->registerPlugin("function","hello","say");//註冊塊$tpl->registerPlugin("block","world","test");/** * 定義函數,參數為數組放置標籤的屬性 * @param 傳遞參數 $args */function say($args){$html="";for ($i=0;$i<$args["num"];$i++){$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$args["content"]."</font><br>";}return $html;}/** * 定義塊,參數為數組放置標籤的屬性 * @param 傳遞參數 $args * @param 傳遞內容 $content * @param 預留引用變數 $a * @param 預留引用變數 $b */function test($args,$content,&$a,&$b){$html="";for ($i=0;$i<$args["num"];$i++){$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$content."</font><br>";}return $html;}//模板檔案名稱可以隨便定義:比如:mysmarty.tpl只有內容是html就可以了$tpl->display("mysmarty.html");?>

mysmarty.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!--{$title}--></title></head><body><!--{hello size=5 color=$color content="$content" num=5 }--><!--{hello size=5 color=$color content="`$arr.one`" num=5 }--><!--{world size=8 color="red" num=4 }-->world<!--{$title}--><br><!--{/world}--></body></html>

二:在smarty外掛程式檔案夾plugins中加入新檔案定義函數和塊

mysmarty.php

<?php/** *  * 以下所有使用變數都要基於設定的首碼和尾碼<!--{}--> * 函數使用 *  * 外掛程式形式寫函數 * 在plugins檔案夾來定義 * 以function開頭的表示函數:function.hello.php的函數名smarty_function_hello($args,$smarty) * 以block開頭的表示塊:block.world.php的函數名smarty_block_world($args,$content,&$smarty) *  *  *///如果檔案載入失敗require會停止繼續解析php;而include則會繼續向下執行require 'init.smarty.php';$title="這是一個文字標題";$tpl->assign("title",$title);$tpl->assign("color","pink");$tpl->assign("content","content");$tpl->assign("arr",array("one"=>"arr"));//模板檔案名稱可以隨便定義:比如:mysmarty2.tpl只有內容是html就可以了$tpl->display("mysmarty2.html");?>

mysmarty.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!--{$title}--></title></head><body><!-- smarty外掛程式檔案夾中的自訂函數 --><!--{html_select_date start_year="1980" end_year="2020"}--><br><!--{hello size=5 color=$color content="$content" num=5 }--><!--{hello size=5 color=$color content="`$arr.one`" num=5 }--><!--{world size=8 color="red" num=4 }-->world<!--{$title}--><br><!--{/world}--></body></html>

在smarty外掛程式檔案夾plugins下的:

function.hello.php(注意命名規則)

<?php/** * 定義函數,參數為數組放置標籤的屬性 * @param 傳遞參數 $args */function smarty_function_hello($args,&$smarty){$html="";for ($i=0;$i<$args["num"];$i++){$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$args["content"]."</font><br>";}return $html;}?>

block.world.php(注意命名規則)

<?php/** * 定義塊,參數為數組放置標籤的屬性 * @param 傳遞參數 $args * @param 傳遞內容 $content * @param 預留引用變數 $a * @param 預留引用變數 $b */function smarty_block_world($args,$content,&$smarty){$html="";for ($i=0;$i<$args["num"];$i++){$html.='<font size=">'.$args["size"].'" color="'.$args["color"].'">'.$content."</font><br>";}return $html;}?>


聯繫我們

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