咨询电话:
15628812133
27
2017/08

html模板生成静态页面及模板分页处理

发布时间:2017-08-27 17:45:47
发布者:pengyifeng
浏览量:
0

它只让你修改页面的某一部分,当然这“某一部分”是由你来确定的。美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种代码是Dreamwerver为了方便自己设计而弄的标识),把这个模板中我们需要改变的地方用一个与HTML可以区分的字符代替,如“{title}”、“[title]”。在生成静态页面的时候只需要把数据和这些字符串替换即可。这就是模板的含义了。

新建一个php页面和一个html页面[模板页];注:如果是从数据库调用数据,则将数据以数组的形式保存,然后循环生成;
在php页面,打开html页面->读取html页面的内容->替换参数->新建(打开)一个新的html页面->将替换的内容写入新文件中->关闭新文件->生成成功;

$open = fopen("template.htm","r"); //打开模板文件
$content = fread($open,filesize("template.htm")); //读取模板文件内容//
print_r($content);$content = str_replace("{title}","测试标题",$content);//替换
$content = str_replace("{contents}","测试内容",$content);
$newtemp = fopen("1.htm","w");//生成,用写入方式打开一个不存在(新)的页面
fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
fclose($newtemp);echo "生成";

如我们指定分页时,每页20篇。某子频道列表内文章经数据库查询为45条,则,首先我们通过查询得到如下参数:1,总页数;2,每页篇数。i++)die(""." role="presentation" style="margin: 0px; padding: 0px; display: inline; line-height: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255); position: relative;">die("".filename."成功!";这句去掉,放到循环后的显示,因为该语句将中止程序执行。例:

$fp= fopen ("temp.html","r");
$content= fread ($fp,filesize ("temp.html"));
$onepage= ’20’;
$sql    = "select id from article where channel=’$channelid’";
$query  = mysql_query ($sql);
$num    = mysql_num_rows ($query);
$allpages= ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
    if ($i == 0){
        $indexpath = "index.html";
    } else {
        $indexpath = "index_".$i."html";
    }
    $start = $i * $onepage;
    $list  = ’’;
    $sql_for_page = "select name,filename,title from article where channel=’$channelid’ limit $start,$onepage";
    $query_for_page = mysql_query ($sql_for_page);
    while ($result = $query_for_page){
        $list .= ’’.$title.’
’;     }     $content = str_replace ("{articletable}",$list,$content);     if(is_file ($indexpath)){         @unlink ($indexpath); //若文件已存在,则删除     }     $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件     if (!is_writable ($indexpath)){         echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo     }     if (!fwrite ($handle,$content)){   //将信息写入文件         echo "生成文件".$indexpath."失败!"; //修改为echo     }     fclose ($handle); //关闭指针 } fclose ($fp); die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
返回列表