smarty中批量生成html的方法
时间:2008-10-14
来源:互联网
首先在smartt.class.php中为Smarty类添加这么一个方法:
//参数一:html文件保存路径,参数2:写入的内容
function MakeHtmlFile($file_name, $c)
{
if(!$fp = fopen($file_name, "wa"))
{
echo "文件打开失败!";
return false;
}
if(!fwrite($fp, $c))
{
echo "文件写入失败!";
fclose($fp);
return false;
}
fclose($fp);
}
问题:我的所有文章都调用的是news.tpl这个模板,那么究竟如何批量生成呢?
我们先看看news.php
[php]
<?php
include_once("config.php");
include_once("init.php");
$s->assign("title","所有的新闻分类");
$ID=$_GET["ID"]+0;
$sql="select * from artical where newsID=$ID";
$rs=$db->fetch($sql);
$s->assign("news",$rs["rec"][0]);//注意:$rs["rec"][0]是个数组
$s->display("news.html");
?>
[/php]
整个模板变量就只有$news,这么写有什么好处呢?我可以以数组的方式读取内容
那么究竟如何生成:
看如下代码:
很简单的
[php]
<?php
include_once("config.php");
include_once("init.php");
$sql="select * from artical";
$rs=$db->fetch($sql);
foreach ($rs["rec"] as $k=>$v)
{
$s->assign("news",$v);
$s->MakeHtmlFile("./news/news_".$v[0].".html",$s->fetch("news.html",null, null, false));
}
?>
[/php]
很简单把!
模板文件:
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title><{$news.titles}></title>
<link href="<{$docroot}>/CSS/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px">
<tr>
<td height="25" colspan="6" align="center" bgcolor="#eeeeee"><strong><{$news.titles}></strong></td>
</tr>
<tr>
<td width="74" height="25" align="right">作者:</td>
<td width="220"><{$news.author}></td>
<td width="40" align="right">时间:</td>
<td width="134" align="left"><{$news.sj}></td>
<td width="110" align="right">关键词:</td>
<td width="220" align="left"><{$news.keyword}></td>
</tr>
<tr>
<td height="25" align="right" >核心提示:</td>
<td height="25" colspan="5" class="artical" style="border:double #FF0000 3px;"><{$news.sumary}></td>
</tr>
<tr>
<td height="25" colspan="6" align="left" class="artical" style="padding:10px;"><{$news.contents}></td>
</tr>
</table>
</body>
</html>
[/php]
总结:主要使用smarty的fetch方法,和文件的读写操作
作者: dongxin1390008 发布时间: 2008-10-14
作者: 网鬼 发布时间: 2008-10-14
作者: gently 发布时间: 2008-10-14
作者: laopi 发布时间: 2008-10-14
如果是虚拟主机的话,还是不要直接生成静态页比较好
太浪费硬盘资源了
作者: quanhaier 发布时间: 2008-10-15
作者: dongxin1390008 发布时间: 2008-10-15
做一个独立的函数不可以吗?
作者: 0hudu 发布时间: 2008-10-15

作者: 菜刀 发布时间: 2008-11-03


作者: justforphp 发布时间: 2008-11-04



作者: 0hudu 发布时间: 2008-11-05
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28