自定義函數 html_checkboxes 根據給定的數據建立複選按鈕組.php
該函數能夠指定哪些元素被選定. 要麼必須指定 values 和 ouput 屬性,要麼指定 options 替代. 全部的輸出與 XHTML 兼容html
html_checkbox用來用給定的數據建立checkbox。name表示checkbox的名稱,values表示checkbox的值,output表示checkbox的顯示,selected表示被選選項的值,options表示一組checkbox的值和顯示,separator表示分割每一個checkbox的符號,labels表示給輸出添加標籤,默認爲true。函數
$arr1=array("a"=>"aaaa","b"=>"bbbb","c"=>"ccccc","d"=>"dddd"); $arr2=array(a,b); $smarty->assign("arr1",$arr1); $smarty->assign("arr2",$arr2); $smarty->display("login.html"); <{html_checkboxes name=hobby options=$arr1 selected=$arr2 }>
根據給定的數據建立選項組. 該函數能夠指定哪些元素被選定. 要麼必須指定 values 和 ouput 屬性,要麼指定 options 替代.ui
index.php:spa
require('Smarty.class.php'); $smarty = new Smarty; $smarty->assign('cust_options', array( 1001 => 'Joe Schmoe', 1002 => 'Jack Smith', 1003 => 'Jane Johnson', 1004 => 'Charlie Brown')); $smarty->assign('customer_id', 1001); $smarty->display('index.tpl');
index.html:插件
<select name=customer_id> {html_options options=$cust_options selected=$customer_id} </select>
用於建立日期下拉菜單. 它能夠顯示任意年月日.code
prefix定義各個下拉列表名字的前綴,默認爲Date_。time決定使用的時間,默認是當前時間。start_year決定下拉列表開始的年份,能夠用年份表示,也能夠用與當前年份的相對年數來表示。默認是當前年份。end_year決定下拉列表結束的年份,能夠用年份表示,也能夠用與當前年份的相對年數來表示。默認是當前年份。display_days決定是否顯示日期。display_months決定是否顯示月份。display_years決定是否顯示年份。month_format決定顯示月份的格式,默認爲%B。day_format決定顯示日期的格式,默認爲%02d。day_value_format決定日期值的格式,默認爲%d。month_value_format決定月份值的格式,默認爲%m。year_as_text決定是否將年份按文本格式輸出。reverse_years決定是否反向輸出各年份。field_array用來取得一組變量,能夠用name[Day],name[Month],name[Year]的方式從form取得得到的值。day_size,month_size,year_size添加大小標籤。all_extra,day_extra,month_extra,year_extra添加額外的屬性到select或input標籤。field_order決定年月日下拉列表的順序,默認爲MDY。field_separator不一樣下拉列表之間的分隔符,默認是\n。year_empty,month_empty,day_empty是在各下拉列表第一欄顯示的內容。orm
index.html:htm
<{ html_select_date start_year=2000 end_yead=2020 }>
用於建立時間下拉菜單. 它能夠顯示任意時分秒對象
prefix定義各個下拉列表名字的前綴,默認爲Time_。time決定使用的時間,默認是當前時間。display_hours決定是否顯示小時。display_minutes決定是否顯示分鐘。display_seconds決定是否顯示秒數。display_meridian 決定是否顯示上午或下午,即顯示am/pm。use_24_hours 決定是否24小時制。minute_interval 決定分鐘之間的間隔。second_interval 決定秒數之間的間隔。field_array用來取得一組變量,能夠用name[Hour],name[Minute],name[Second]的方式從form取得得到的值。all_extra,hour_extra,minute_extra,second_extra ,meridian_extra添加額外的屬性到select或input標籤。
index.html:
<{html_select_time use_24_hours=true}>
找到存放函數插件的文件夾在裏面新建文件:function.函數名.php (block.函數名.php)
在該文件裏面新建一個方法:
普通函數: function smarty_function_函數名($args){}
塊函數 : function smarty_block_函數名($args,$nr,$smarty,$bs){}
參數$args:調用該函數傳入的屬性關聯參數
參數$nr:block塊之間所夾的內容
參數$smarty:對象
參數$bs:是不是第一次調用(開始標記裏面調用)快函數用
該方法最終有返回值
普通插件函數
路徑
在libs/plugins文件夾下 創建一個php文件
這裏咱們能夠編寫一個插件函數,可是這個函數名和文件名有一個規範,必須遵照
文件名的格式:function.自定義函數名.php
裏面的代碼以下 <?php function smarty_function_hsp($args, &$smarty){ $str=""; for($i=0;$i<$args['times'];$i++){ $str.="<font color='".$args['color']."' size='".$args['size']."'>".$args['con']."</font>"."<br>"; } return $str; } ?>
模板調用
<{hsp times="10" size="5" color="green" con="hello,world"}>
塊函數
這裏以塊的方式增長一個插件,這裏一樣要保持名字的規範
文件名的格式:block.塊名.php
路徑
在libs/plugins文件夾下創建一個名爲 block.test.php的文件
<?php function smarty_block_test($args, $con, &$smarty){ $str=""; for($i=0;$i<$args['times'];$i++){ $str.="<font color='".$args['color']."' size='".$args['size']."'>".$con."</font>"."<br>"; } return $str; } ?>
模板調用
<{test times="10" size="5" color="yellow"}> hello,world <{/test}>