smarty 生成静态html页面

作者:袖梨 2022-07-02

//smarty 下载我主不说了可以到down.111com.net免费下载哦。
$tpl = new Smarty();
$tpl->template_dir = dirname(__FILE__);
$tpl->compile_dir = '111com.net';

$tpl->compile_check = false;//$cfg['debug'];
$tpl->debugging = 0;
$tpl->caching = 0;
$tpl->cache_lifetime = 3600;

$tpl->left_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 代码

//本文章原创于转载注明出处

相关文章

精彩推荐