smarty 生成静态html页面
生成静态页面,用php生成会比smarty生成要快,但是如果你用smarty模板做的页面,现在要生成html的话,就可以看看这篇smarty 生成静态html页面教程物。
<?php $tpl = new Smarty(); $tpl->template_dir = dirname(__FILE__); $tpl->compile_dir = 'phprm.com'; $tpl->compile_check = false; //$cfg['debug']; $tpl->debugging = 0; $tpl->caching = 0; $tpl->cache_lifetime = 3600; $tpl->left_delimiter = '<!--{'; $tpl->right_delimiter = '}-->'; //初始化smarty模板 $file = date("Y-m-d") . ".html"; $tempFile = 'template.tpl'; //解析模板文件 $tpl->assign('title', 'php smarty 生成静态html页面'); $tpl->assign('content', '这是文章内容'); $content = $tpl->fetch($tempFile, null, null, false); //关键一步就是把 $content生成html 文件就行了,下面来看 $dir = dirname($file); //目录不存在就创建 if (!file_exists($dir)) { creatDir($dir); } $handle = fopen($file, "w+"); //创建文件 if (!is_writable($file_name)) { //判断写权限 echo '没有写权限'; } if (!fwrite($handle, $content)) { echo '文件不可写'; } fclose($handle); //关闭指针 echo '生成静态页面成功'; //返回文件名 function creatDir($dir) { if (!is_dir($dir)) { if (!creatDir(dirname($dir))) { return false; } if (!mkdir($dir, 0777)) { return false; } } return true; }
//tmplate.tpl 代码
<!--{$title}-->
<!--{$content}-->
文章地址:http://www.phprm.com/frame/smarty.html
转载随意^^请带上本文地址!