查看: 5617|回复: 0
打印 上一主题 下一主题

Smarty中批量生成html的方法

[复制链接]
跳转到指定楼层
1#
发表于 2008-11-10 20:40:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
经过一番摸索,终于找到方法,写出来大家指点一下:
首先在smartt.class.php中为Smarty类添加这么一个方法:
PHP代码:
//参数一: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");
?>


整个模板变量就只有$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代码:
<!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>


总结:主要使用smarty的fetch方法,和文件的读写操作

详细内容:http://bbs.phpchina.com/thread-84144-1-3.html
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

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