六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

smarty学习1

[复制链接]

升级  73.25%

801

主题

801

主题

801

主题

探花

Rank: 6Rank: 6

积分
2465
 楼主| 发表于 2013-1-27 05:01:28 | 显示全部楼层 |阅读模式
  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}
这里进行调用
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表