smarty中的html_options方法使用简介

smarty html_options 设置默认select 选值

模板中html_options接受的其实是一个数组,并且这个数组还是有点要求滴~
数组的键值将是option的value值,而数组的值则将是option的显示值,如果需要初始状态某个值是出于选择状态,那么

index.php:

require('Smarty.php.class');
$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.tpl:

<select name=customer_id>
 {html_options options=$cust_options selected=$customer_id}
</select>

效果:

<select name=customer_id>
 <option value="1000">Joe Schmoe</option>
 <option value="1001" selected="selected">Jack Smith</option>
 <option value="1002">Jane Johnson</option>
 <option value="1003">Charlie Brown</option>
</select>

html_options还有一种用法,option的value和显示是分开的,这样方便处理其他的一些情况
具体用法如下
index.php:

require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array('Joe Schmoe','Jack Smith','JaneJohnson','Carlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');

模板index.tpl:

<select name=customer_id>
{html_options values=$cust_ids selected=$customer_id output=$cust_names}
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值