我想在网站中加个导出静态网页的功能,有什么好的方案可以快速导出呢?
`ob_start();
//模板处理
//echo 模板内容
$content = ob_get_contents();
ob_end_clean();
file_put_contents('./demo.html', $content);`
也可以用smart模板实现,如下所示:
<?php
require('smarty/Smarty.class.php');
$t = new Smarty;
$t->assign("title","Hello World!");
$content = $t->fetch("templates/index.htm");
//这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
使用也很简便。
<?php
/* 在这里数据库增删改查之前对缓存进行过期判断和应用 */
$app['data'] = db_crud();
$view = render('index.php');
function render($template) {
global $app;
ob_end_clean();
ob_start();
require APP_ROOT.'/view/'.$template; //模板里会用到数据$app['data']
$html = ob_get_contents();
ob_end_clean();
ob_start();
/* 在这里把 ob_get_contents 拿到的字符串 file_put_contents 写入文件系统 */
return $html;
}
1 回答4.1k 阅读✓ 已解决
3 回答1.9k 阅读✓ 已解决
2 回答2.3k 阅读✓ 已解决
1 回答1.4k 阅读✓ 已解决
2 回答2.2k 阅读
1 回答669 阅读✓ 已解决
2 回答582 阅读✓ 已解决
不是有个file_put_content()的函数吗?