smart原则|smarty 基础入门

时间:2015-05-13  来源:smarty模板  阅读:

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




<{section name=loop loop=$new}>
<{$new[loop].id}>
<{$new[loop].title}>

<{/section}>



现在说明下: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}>

<{/section}>

smart原则|smarty 基础入门

http://m.bbyears.com/jiaocheng/15047.html

推荐访问:smarty模板引擎 smarty教程 smarty模板
相关阅读 猜你喜欢
本类排行 本类最新