现在的位置: 首页 > 综合 > 正文

smarty学习1

2013年03月05日 ⁄ 综合 ⁄ 共 999字 ⁄ 字号 评论关闭

  php里的smarty是个不错的模版,是个好东西来的,最近项目要用到,找了本packet publishing出的
smarty of php的书来看,很薄,不错的书,现将主要内容等笔记之

1 安装
    下载后的 smarty 解压缩后,将lib目录放到当前php项目的根目录下就可以了,然后还要
建立templates目录存放模版,template_c目录用来存放模版产生的文件

2  建立tpl模版
   在templates目录下建立index.tpl模版,假设是用列表的形式列出数据库内容

 {foreach item=huojiang from=$huojiangs}
              <tr>
                <td width="60%">{$huojiang.nickname}</td>
                <td align="center" width="40%">{$huojiang.score}</td>
              </tr>
   {/foreach}
看到了吧,一个{foreach}就OK了,接下来,先看调用文件index.php
<?

require("libs/Smarty.class.php");
$smarty = new Smarty();

//.....数据库连接代码

//设置一个数组
$huojiangs=array();

     $jifensocre= mysql_query("..............................................");
      while ($arr=mysql_fetch_assoc($jifensocre)) 
               {
       $huojiangs[]=$arr;

    }
$smarty->assign("huojiangs",$huojiangs);        //assign
$smarty->display("index1.tpl");              //compile and render the output  
     ?>
$smarty->assign("huojiangs",$huojiangs);   这句中,把数组赋值给huojiangs了,
然后在模版里,用
 {foreach item=huojiang from=$huojiangs}
这里进行调用

抱歉!评论已关闭.