Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28543337
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类:

2009-07-23 14:03:50

用Smarty模板生成html文件,其实就是在 display() 的同时,增加了 ob_start()、b_get_contents() 和 fwrite() 函数。

具体实现方法,请看下面两个文件:

文件一: generate_html.php

 require_once('inc/smarty.inc.php');
 require_once('include/config.php');

 ob_start();
 
 $title         = "title";
 $description     = "description";
 $keywords         = "keywords";
 $outfilename = "test.html";

 $smarty->assign("TITLE",           $title);
 $smarty->assign("DESCRIPTION",     $description);
 $smarty->assign("KEYWORDS",        $keywords);
 $smarty->assign("CSSPATH",            "/css");
 $smarty->assign("TPL_LEFT",         TPL_LEFT);
 $smarty->assign("TPL_RIGHT",         TPL_RIGHT);
 $smarty->assign("TPL_TOP",         TPL_TOP);
 $smarty->assign("TPL_FOOTER",         TPL_FOOTER);
 $smarty->assign("TPL_CENTER",         TPL_CATEGORY);

 $smarty->display(TPL_MAIN); // TPL_MAIN 等常量在 include/config.php 中已经被定义

 $str = ob_get_contents();

 $fp = @fopen($outfilename, 'w');
 if (!$fp) {
     Show_Error_Message( ERROR_WRITE_FILE );
 }
 fwrite($fp, $str);
 fclose($fp);
 ob_end_clean();
?>

文件二: templates/main.htm



<{config_load file="global.conf"}>
<{$TITLE}>







   
       
   
   
       
       
       
   
   
       
   

        <{include file="$TPL_TOP"}>
       

        <{include file="$TPL_LEFT"}>
       

        <{include file="$TPL_CENTER"}>
       

        <{include file="$TPL_RIGHT"}>
       

        <{include file="$TPL_FOOTER"}>
       



阅读(888) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~