码迷,mamicode.com
首页 > Web开发 > 详细

Smarty 函数讲解

时间:2016-07-28 20:25:48      阅读:514      评论:0      收藏:0      [点我收藏+]

标签:

这里给大家总结了几种Smarty 函数并分别详细讲解了。如果你正在学习Smarty  希望这篇文章对你有用。

 

html_checkboxes

自定义函数 html_checkboxes 根据给定的数据创建复选按钮组.

该函数可以指定哪些元素被选定要么必须指定 values 和 ouput 属性,要么指定 options 替代所有的输出与 XHTML 兼容

 

html_checkbox用来用给定的数据创建checkboxname表示checkbox的名称,values表示checkbox的值,output表示checkbox的显示,selected表示被选选项的值,options表示一组checkbox的值和显示,separator表示分割每个checkbox的符号,labels表示给输出添加标签,默认为true

 

Smarty 函数详解

 

html_checkbox用来用给定的数据创建checkboxname表示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 }>

 

html_options

根据给定的数据创建选项组该函数可以指定哪些元素被选定要么必须指定 values 和 ouput 属性,要么指定 options 替代.

Smarty 函数详解

 

index.php:

 

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>

html_select_date

用于创建日期下拉菜单它可以显示任意年月日.

prefix定义各个下拉列表名字的前缀,默认为Date_time决定使用的时间,默认是当前时间。start_year决定下拉列表开始的年份,可以用年份表示,也可以用与当前年份的相对年数来表示。默认是当前年份。end_year决定下拉列表结束的年份,可以用年份表示,也可以用与当前年份的相对年数来表示。默认是当前年份。display_days决定是否显示日期。display_months决定是否显示月份。display_years决定是否显示年份。month_format决定显示月份的格式,默认为%Bday_format决定显示日期的格式,默认为%02dday_value_format决定日期值的格式,默认为%dmonth_value_format决定月份值的格式,默认为%myear_as_text决定是否将年份按文本格式输出。reverse_years决定是否反向输出各年份。field_array用来取得一组变量,可以用name[Day],name[Month],name[Year]的方式从form取得获得的值。day_sizemonth_sizeyear_size添加大小标签。all_extraday_extramonth_extrayear_extra添加额外的属性到selectinput标签。field_order决定年月日下拉列表的顺序,默认为MDYfield_separator不同下拉列表之间的分隔符,默认是\nyear_emptymonth_emptyday_empty是在各下拉列表第一栏显示的内容。

index.html:

<{ html_select_date start_year=2000 end_yead=2020 }>

 

html_select_time 

用于创建时间下拉菜单它可以显示任意时分秒

prefix定义各个下拉列表名字的前缀,默认为Time_time决定使用的时间,默认是当前时间。display_hours决定是否显示小时。display_minutes决定是否显示分钟。display_seconds决定是否显示秒数。display_meridian 决定是否显示上午或下午,即显示am/pmuse_24_hours 决定是否24小时制。minute_interval 决定分钟之间的间隔。second_interval 决定秒数之间的间隔。field_array用来取得一组变量,可以用name[Hour],name[Minute],name[Second]的方式从form取得获得的值。all_extrahour_extraminute_extrasecond_extra meridian_extra添加额外的属性到selectinput标签。

index.html:

<{html_select_time use_24_hours=true}>

自定义Smarty函数

找到存放函数插件的文件夹在里面新建文件:function.函数名.php (block.函数名.php)


在该文件里面新建一个方法:

普通函数:  function smarty_function_函数名($args){}
块函数     function smarty_block_函数名($args,$nr,$smarty,$bs){}
参数$args:调用该函数传入的属性关联参数
参数$nrblock块之间所夹的内容
参数$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}>

 

 

 

原文来自:博客园/小兵

Smarty 函数讲解

标签:

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
jiangjie190
加入时间:2016-02-19
  关注此人  发短消息
文章分类
jiangjie190”关注的人------(0
jiangjie190”的粉丝们------(0
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!