Smarty生成静态页面的方法

本文介绍如何使用Smarty模板引擎生成静态页面。通过配置Smarty类并利用fetch()方法将模板内容输出到指定HTML文件中,实现页面的静态化。文章提供了一个简单的示例,包括Smarty配置、模板文件和最终生成的静态页面。
<?php  
/* 
 *Smarty生成静态页面 
 *Smarty生成静态页面的条件 
 *调用Smarty类及配置相关属性 
 *使用fetch()方法实现生成静态页面 
 */  
  
//调用Smarty类  
include("smarty/smarty.class.php");  
  
//实例化Smarty和配置Smarty属性  
$smarty = new Smarty();    //实例化Smarty对象  
$smarty->template_dir = "smarty/templates";    //模板文件的目录  
$smarty->compile_dir = "smarty/template_c";    //编译的模板文件  
$smarty->config_dir = "smarty/configs";        //配置文件目录  
$smarty->cache_dir = "smarty/cache";           //缓存的所有文件  
$smarty->caching = false;  
$smarty->left_delimiter = "<{";  
$smarty->right_delimiter = "}>";  
  
//Smarty生成静态页面的代码  
$smarty->assign("contect", "HelloWorld!");  
$contect = $smarty->fetch("index.tpl");  
$fp = fopen("index.html", "w");  
fwrite($fp, $contect);  
fclose($fp)  
?>  
  
//index.tpl模版  
<html>  
<head>  
<title>Test</title>  
</head>  
<body>  
<{$contect}>  
</body>  
</html>  
  
//生成后的index.html  
<html>  
<head>  
<title>Test</title>  
</head>  
<body>  
HelloWorld!  
</body>  
</html>  

 

转:http://blog.youkuaiyun.com/l_phper/article/details/4851802

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值