您的位置:首页 > 编程语言 > PHP开发

php+smarty生成静态页面详解

2008-08-06 17:12 495 查看
<?php
/*
以下是我在工作中对php生成静态页的一种方法,以下程序,如有任何不明白的地方,可到我的网站给我留言。
www.wuvale.com 愿意和你一起探讨。
*/

require('libs/Smarty.class.php');
$tpl=new Smarty();
$tpl->template_dir='./templates/';
$tpl->compile_dir='./templates_c';
$tpl->config_dir='./config/';
$tpl->cache_dir='./cache/';
$tpl->left_delimiter='<{';
$tpl->right_delimiter='}>';

ob_start(); //打开输出缓冲区

$tpl->assign('s_title',$_POST['title']);//设置网站标题

//以下为接受传递过来的变量并赋值到模板页

$tpl->assign("title",$_POST['title']);
$tpl->assign("content",stripslashes($_POST['content']));
$tpl->assign("time",date("Y-m-d"));

$tpl->display("tpl.html");

$this_my_f=ob_get_contents();//读取缓冲区数据

ob_end_clean();//清空缓冲区数据

//--------------------------创建文件夹-----------------------------------
$dir_name =date("Ymd"); //以当前日期,创建应该生成的静态页面所要存入的目录

if (!is_dir("webpage/".$dir_name)) //先判断是否已经创建了此目录!无,则先创建此目录
{
mkdir("webpage/".$dir_name);
}

$filename ="tpl.html"; //-----------------------------------静态页保存的路径--------------------

if(tohtmlfile_cjjer($filename,$this_my_f)){
echo ("生成页面成功");
}else{
echo ("<script>alert('生成静态页面失败!')</script>")
}

?>

/***********************************************************************
+ ---------------------------------------------------------------------
+ 生成静态文件的过程函数
+ ---------------------------------------------------------------------
************************************************************************/
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content)
{

//$dir_name =date("Ymd"); //以当前日期,创建应该生成的静态页面所要存入的目录

//if (!is_dir($dir_name)) //先判断是否已经创建了此目录!无,则先创建此目录
//{
//mkdir($dir_name);
//}

if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name);
}
$cjjer_handle = fopen ($file_cjjer_name,"w");
if (!is_writable ($file_cjjer_name)){
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //关闭指针
return $file_cjjer_name;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: