php生成静态页面的简单实例
一个简单的实例:新闻模版文件news_tmp.html:
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<TABLE border=0 width=767 cellspacing="0" cellpadding="5">
<TR>
<TD>
<div align="center">{news_title}</div>
</TD>
</TR>
<TR>
<TD>
<div align="center">{news_image}</div>
</TD>
</TR>
<TR>
<TD>{news_content}</TD>
</TR>
</TABLE>
</body>
</html>
______________________________________________________________________________________________
答7:
新闻生成文件aaa.php:
<?
//假设下面信息都来自表单
$title="娱乐新闻---郑秀文准备宣布退出娱乐圈";
$news_title="郑秀文准备宣布退出娱乐圈";
$image_path=array("image/xxx.jpg","image/xxx2.jpg","image/xxx3.jpg");
$news_content="事业如日方中的郑秀文(Sammi),除是乐坛天后外,亦以五百五十万片酬登上全港片酬最高女星之位......";
$fp=@fopen("news_tmp.html","r") or die("没有模版文件或者没有相关权限!");
$str=fread($fp,filesize("news_tmp.html"));
fclose($fp);
______________________________________________________________________________________________
答8:
$news_filename=time().".html";//生成的新闻文件名
$str=str_replace("{title}",$title,$str);
$str=str_replace("{news_title}",$news_title,$str);
$str=str_replace("{news_content}",$news_content,$str);
if(count($image_path)){
for($n=0;$n<count($image_path);$n++)
$news_image.="<img src=".$image_path[$n]."><BR>";
$str=str_replace("{news_image}",$news_image,$str);
}else
$str=str_replace("{news_image}","",$str);
$fw=fopen($news_filename,"w");
fwrite($fw,$str);
?>
简单演示PHP如何使用模板生成静态页面。
模板文件templets.htm:
<html>
<head>
<title>{title}</title>
</head>
<body>
<p>Hello {hello}</p>
</body>
</html>
PHP文件代码:
<?php
$title = 'webjx';
$hello = 'webjxcom!';
$file = file_get_contents('templets.htm');
$file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
echo $file;
?>
查看(48) 评论(0) 收藏 推荐
www.happyjiaoyour.com域名出售
2007-11-20 11:21:18
www.happyjiaoyour.com域名出售,如有意向,请加qq:371606962
查看(12) 评论(1) 收藏 推荐
第一个smarty例子--分页显示数据
2007-11-18 14:18:21
查看PHP-smarty安装
模板页index.tpl:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>查看留言</title>
<styletype="text/css">
<!--
.STYLE1{color:#FF0000}
-->
</style>
</head>
<body>
<{*assignvar="login"value=0*}>
<divalign="center">
<p><ahref="index.php">主页</a> <ahref="addmeg.php">留言</a>
<{if$login==0}> <ahref="gli.php">管理</a><{/if}> <{*login=0未登陆,显示管理链接*}>
</p>
</div>
<{sectionname=lploop=$ly}>
<tablewidth="590"border="1"align="center"cellpadding="0"cellspacing="1">
<tr>
<tdwidth="85"rowspan="2"><p>留言人:<br/>
<{$ly.fbr}></p>
</td>
<tdwidth="427"height="23"><p> 标题:<{$ly.tm}></p>
</td>
</tr>
<tr>
<tdheight="58"> 内容:<{$ly.nr}></td>
</tr>
</table><br/>
<{/section}>
<palign="center">总<spanclass="STYLE1"><{$pcunt}></span>页 当前为第<spanclass="STYLE1"><{$page}></span>页 <ahref="index.php">首页</a> <{$qian}><{$next}><ahref="index.php?page=<{$pcunt}>">最后一页</a></p>
</body>
</html>
//////////////////////////////////////////////////////////////////////////////
index.php:
<?php
/*********************************************
*
*文件名:index.php
*作用:显示留言分页
*作者:龙的心
*QQ:282129207
*
*********************************************/
require("./class/Smarty.class.php");//包含smarty类文件
$smarty=newSmarty();//建立smarty实例对象$smarty
$smarty->template_dir='./templates/';
$smarty->compile_dir='./templates_c/';
$smarty->config_dir='./configs/';
$smarty->cache_dir='./cache/';
$smarty->caching=false;//这里是调试时设为false,发布时请使用true
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
mysql_connect('localhost','root','root');
mysql_select_db('nihao');
mysql_query("setnames'gb2312'");
$page=$_GET['page'];
if($page==null)
$page=1;
$psize=4;//每页记录数
$str="select*fromly";
$query=mysql_query($str);
$num=@mysql_num_rows($query);//总记录数
$pcunt=ceil($num/$psize);//总页数
$nextpage=$page+1;
$qianpage=$page-1;
$start=($page-1)*$psize;
$str="select*fromlylimit$start,$psize";
$query=mysql_query($str);
while($arr=mysql_fetch_array($query))
{//print_r($arr);
$array[]=$arr;
}
if($page>1)$str1="<ahref=index.php?page=$qianpage>上一页</a> ";
if($page<$pcunt)$str2="<ahref=index.php?page=$nextpage>下一页</a> ";
$smarty->assign("login","0");//login=0未登陆,显示管理链接
$smarty->assign("ly",$array);
$smarty->assign("page",$page);
$smarty->assign("qian",$str1);
$smarty->assign("next",$str2);
$smarty->assign("pcunt",$pcunt);
$smarty->display("index.tpl");
?>
页:
[1]