php smarty 缓存和配置文件的基本使用方法

本文介绍了Smarty模板引擎中的缓存机制与配置文件调用方法。通过实例演示了如何设置缓存目录、缓存时间等参数,并展示了如何从配置文件加载全局及局部变量到模板中。

smarty高级部分包括缓存机制和配置文件的调用

下面是代码实现:

文件一,配置文件:

#全局变量
title="网站主页"
content="一个网站的主体部分"


#在news中
[news]
title="新闻页面"
content="新闻内容"


#在forum中
[news]
title="论坛页面"
content="论坛内容"

文件二,php文件:

<?php
    require("Smarty.class.php");
    $smarty = new Smarty();
    $smarty -> template_dir = "./templates"; //模板存放目录 
    $smarty -> compile_dir = "./templates_c"; //编译目录 
    $smarty -> cache_dir = "./cache"; //缓存目录
    $smarty -> config_dir = "./configs"; //缓存目录
    
    $smarty -> caching = 1;//打开缓存功能
    $smarty -> cache_lifetime = 300;//缓存的时间

    
    $smarty -> display('caching_conf.tpl');

?>

文件三,模板文件:

{config_load file="smarty.conf"}
<html>
<head>
<title>{#title#}</title>
</head>
<body>

{#content#}

</body>
</html>

这样,title和content的地方就被换成“网站主页”和“一个网站的主体部分”。

如果{config_load file="smarty.conf" section="news"},

section表示读取哪部分的局部变量以供目前模板使用。

以上红色部分加载的是news部分的局部变量,title和content被换成新闻页面和新闻内容。

转载于:https://www.cnblogs.com/wuyou/p/3390271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值