教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php smarty模板生成静态html页面代码

php smarty模板生成静态html页面代码

发布时间:2023-05-11   编辑:jiaochengji.com
教程集为您提供php smarty模板生成静态html页面代码等资源,欢迎您收藏本站,我们将为您提供最新的php smarty模板生成静态html页面代码资源
 代码如下

include('./www.111cn.net/smarty/smarty.class.php');
$smarty = new smarty();
$smarty->template_dir = "templates/";
$smarty->compile_dir = "templates_c/";
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";

$db = mysql教程_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names "utf8"');
$result = mysql_query('select * from news');
while($row[] = mysql_fetch_array($result)){
 $smarty->assign('news',$row);
}
//$smarty->display('smarty_html.html');
//以上不用写注释你都能看懂
//这里我们不显示他
//而是获得他
$content = $smarty->fetch('smarty_html.html');
//获得smarty替换都的模板文件内容也就是display之后的smarty_html.php的内容
/*
 其实这一步就相当于这样
 ob_start(); 开启缓冲区
 $smarty->display('smarty_html.html');
 $content = ob_get_contents(); 获得缓冲区内容
 ob_end_claen;关闭缓冲区
*/
makehtml('news.html',$content);//写入内容到news.html文件
echo '<a href="news.html">查看</a>';
//点击查看静态页面就生成成功了!
//但是有个问题就是必须在当前页面才行
//比如你在另外一个php文件里$smarty->fetch('smarty_html.html');
//只能得到原本的模板文件内容,因为没有assign所以所有的模板变量都没替换
//生成了也是费的!
function makehtml($file,$content){
 $fp = fopen($file,'w');
 fwrite($fp,$content);
 fclose($fp);
}
?>

//模板页面

 代码如下
<!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=utf-8" />
<title>无标题文档</title>
</head>
<body>
<{section name=n loop=$news}>
id  <{$news[n].news_id}><br />
title  <{$news[n].news_title}> <br />
content<{$news[n].news_content}>
<hr />
<{sectionelse}>
暂时没有新闻
<{/section}>
</body>
</html>

您可能感兴趣的文章:
有关smarty模板引擎生成静态页的关键代码
php模版生成html的小例子
smarty获得当前url示例代码
PHP中使用smarty生成静态文件的例子
smarty生成静态页面的方法
php smarty 基础
php生成静态页面的三种方法与代码详解
PHP模板引擎Smarty缓存使用
PHP模板引擎smarty生成随机数 smarty中math函数用法
php Smarty的缓存操作

[关闭]
~ ~