ArthurXF从业10年的CTO,受上海非凡学院邀请,进行PHP,CSS,FREEBSD等网络技能授课培训,有意向参加的,请联系.
{$smarty.now|date_format:"%Y年%m月%d日"} 这样写就成了"2010年%m月%d日"+一些乱码如果给汉字后加上空格就正常了,但是输出也有了空格。

为了解决这个问题,我去读了smarty的插件代码modifier.date_format.php:
发现里面strftime这个PHP函数对于中文支持不好。
所以我修改了modifier.date_format.php函数,一劳永逸啊。大家可以直接复制替换原有内容即可。
而且我这个函数还是支持繁简中文的哦。呵呵
function smarty_modifier_date_format($string, $format = '%b %e, %Y', $default_date = '')
{
   if (substr(PHP_OS,0,3) == 'WIN') {
           $_win_from = array ('%e',   '%T',        '%D');
           $_win_to    = array ('%#d', '%H:%M:%S', '%m/%d/%y');
           $format = str_replace($_win_from, $_win_to, $format);
    }
   $arrTemp = array('年','月','日','时','分','秒','時');
   foreach($arrTemp as $v){
     if(strpos($format,$v)){
       $strFormat = str_replace('%','',$format);
     }
   }
    if($string != '') {
    if(!empty($strFormat)) return date($strFormat, smarty_make_timestamp($string));
    else return strftime($format, smarty_make_timestamp($string));
    } elseif (isset($default_date) && $default_date != '') {
     if(!empty($strFormat)) return date($strFormat, smarty_make_timestamp($default_date));
        else return strftime($format, smarty_make_timestamp($default_date));
    } else {
        return;
    }

}
  ArthurXF是BIWEB WMS开源系统的创始人,关于BIWEB WMS开源系统的介绍,可以访问biweb.cn
  smarty里面的{html_checkboxes}是用来自动生成html的checkbox的,非常方便和实用。例如下面的例子:

$smarty->assign('cust_checkboxes', array(
                                    1000 => 'Joe Schmoe',
                                    1001 => 'Jack Smith',
                                    1002 => 'Jane Johnson',
                                    1003 => 'Charlie Brown')
                                  );
$smarty->assign('customer_id', 1001);

?>  

上面的赋值在模版里写入下面的smarty语句

{html_checkboxes name='id' options=$cust_checkboxes  selected=$customer_id separator='
'}

就可以自动生成下面的html代码。多简单省事啊。










不过当我们需要在下面的input上加onclick事件如何来写呢?手册上也没看到例子,并且写的不明不白。我ArthurXF本以后要去扩展smarty了,谁知道看了源代码之后发现要实现这个功能太容易了。请看我写的例子。

{html_checkboxes name='id' options=$cust_checkboxes  selected=$customer_id separator='
' onclick="alert('ok')"}

这样就可以了,呵呵,是不是非常简单啊?请转载的兄弟姐妹,帮忙推广一下BIWEB WMS开源系统。biweb.cn
在传给smarty的section的数组必须要特别注意,不能使用key为字符串的数组,否则无法输出数组的值。至少我是没找到办法可以把key为字符串的数组的值输出。如果有人有方法输出,可以在下面留言给大家共享一下。
Tags: , ,

  此文章由ArthurXF(肖飞)倾情奉献!
  我们在使用smarty的时候,经常会碰到变量组合成新变量的时候,例如:$aaa$b,$b是可变值,我们需要得到$aaa1,$aaa2,$aaa3等变量.但是在smarty中,这样的变量连接写法是不允许的.

针对这样的问题,我给出下面几种情况的解决方案.
注:<?{和}?>是我定义的smarty定界符,等同于{和},希望大家理解.

引用
1.针对数组的变量连接问题:
<?php
$arrMType = array();
$arrMType[1] = '高交会展';
$arrMType[2] = '学术会议';
$arrData[id] = 1;
?>
在smarty中.
$arrMType[$arrData.id]是错误的.
解决方案:
<?{foreach from=$arrType key=key item=value}?>
<?{if $key == $arrData.type_id}?><?{$value}?><?{/if}?>
<?{/foreach}?>


2.针对文件引用的变量连接问题:
<?PHP
$page="index";
?>
在smarty中,下面的写法都是错误的.
<?{include file=$page.html}?> //这样不能执行成功
<?{include file=$page|cat:".html"}?> //还是不行
解决方案:
<?{include file="`$page`.html"}?>


3.针对函数传递的变量连接问题:
<?PHP
$arrGWeb['module_id'] = 'news';
?>
要在smarty中实现:
<?{url url = "/news/list?type_id=1"}?>
错误的写法:
<?{url url = "/$arrGWeb.module_id/list?type_id=1"}?>
正确的写法:
<?{url url = "/`$arrGWeb.module_id`/list?type_id=1"}?>
注意:"`"这个符号,是数字1前面的那个符号,不是单引号.


ArthurXF从业10年的CTO,受上海非凡学院邀请,进行PHP,CSS,FREEBSD等网络技能授课培训,有意向参加的,请联系.
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]