smarty 基础入门
PHP文件原代码:
<?php include_once("./comm/ Smarty .class.php"); //包含smarty类文件 define("NUM", 5); //定义每次显示的新闻条数 $smarty = new Smarty(); //建立smarty实例对象$smarty $smarty->templates_dir = "./templates"; //设置模板目录 $smarty->compile_dir = "./templates_c"; //设置编译目录 $smarty->cache_dir = "./cache"; //设置缓存目录 $smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间 $smarty->caching = false; //这里是调试时设为false,发布时请使用true $smarty->left_delimiter = "<{"; //设置左边界符 $smarty->right_delimiter = "}>"; //设置右边界符 $db= mysql _connect('localhost','root','123456'); mysql_select_db('liu',$db); $sql="select id,title from lyb"; mysql_query("set names gb2312"); $result=mysql_query($sql,$db); while($row=mysql_fetch_array($result)){ $array[]=array("id"=>$row['id'], "title"=>$row['title']); } mysql_close(); $smarty->assign('new',$array); $smarty->display('3.html'); ?>
3.html
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#B9E9FF"> <tr> <td height="115" width="10"></td> <td valign="top" width="295" bgcolor="#B9E9FF"> <{section name=loop loop=$new}> <{$new[loop].id}> <{$new[loop].title}><br> <{/section}> </td> </tr> </table>
现在说明下:3.php中的东西.
我是用过程写的方面大家看下啊!
$smarty = new Smarty(); //建立smarty实例对象$smarty $smarty->templates_dir = "./templates"; //设置模板目录 这个必须真实存在(HTML文件在这里) $smarty->compile_dir = "./templates_c"; //设置编译目录 这个必须真实存在(编译的目录) $smarty->cache_dir = "./cache"; //设置缓存目录 $smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间 $smarty->caching = false; //这里是调试时设为false,发布时请使用true $smarty->left_delimiter = "<{"; //设置左边界符 $smarty->right_delimiter = "}>"; //设置右边界符 $smarty->assign('new',$array); //这个是new在html中section LOOP值 $smarty->display('3.html'); //这个是哪个HTML文件.
在html中.
<{section name=loop loop=$new}> <{$new[loop].id}> <{$new[loop].title}><br> <{/section}>
文章地址:http://www.phprm.com/frame/ca8a76ffced04f9963992981d3b5d8cc.html
转载随意^^请带上本文地址!