首页 > PHP > Smarty 入手

Smarty 入手

2009年04月04号
查看评论 发表评论 1,440次浏览

另外两个可选参数介绍如下:
scope: 确定所加载的配置变量的作用域。默认情况下设置为local,表示变量只能用于本地模板。其它可能的设置包括 parent 和 global 。作用域设置为 parent 时,变量可用于本地模板和调用模板。作用域设为global 时,变量则可以用于所有模板。
section:指定加载配置文件的特定节。因此,如果只对某个特定节感兴趣,可以只加载该节,而非整个文件。
引用配置变量
配置文件中变量的引用方式与其它变量的引用方式有所不同。实际上,这些配置变量使用几种不同的语法来引用,下面将介绍这个内容。
1、#
在 Smarty 模板中,可以在变量前面加上#号来引用配置变量。例如:
{#title}
2、Smarty的$smarty.config变量
引用配置变量时,如果喜欢更为正式的语法,可以使用 Smarty 的 $smarty.config 变量。例如:
{$smarty.config.title}
3、get_config_vars() 方法
array get_config_vars([string variablename])
get_config_vars() 方法返回一个数组,包含加载的所有配置变量值。如果只对某个变量值感兴趣,可以通过 variablename 传入该变量。例如,如果只对以上 app.config 配置文件中 Aggregation 节的 title 感兴趣,可以首先使用 config_load 函数加载该节:
{config_load file=”app.config” section=”Aggregation”}
然后,在模板中启用PHP的节中调用 get_config_vars( ),如下:
$title = smarty->get_config_vars(“title”);
当然,无论选择哪一种获取配置参数的语法,都不要忘记首先使用 config_load 函数加载配置文件。
Smarty 的缓存
功能强大的应用程序一般都有很大的开销,通常是数据获取和处理操作带来的。对于Web应用程序,这个问题是由于HTTP协议的无状态性造成的。 由于HTTP协议是无状态的,对于每个页面请求,都要重复地执行相同的操作,而不论数据是否修改。要让应用程序在世界范围最大的网络中可用,会使这个问题 进一步恶化。所以,毫不奇怪,人们总在想方设法地让Web应用程序运行得更高效。对此有一种特别有效的解决方案,这也是最合理的方案之一:将动态页面转换 成静态页面,只有在页面内容有修改之后才重新构建,或者定期地重新构建。Smarty提供了这样一个特性,一般称为页面缓存。
如果要使用缓存,需要首先通过设置Smarty 的缓存属性来启用缓存,如下:
<?php
require(“Smarty.class.php”);
$smarty = new Smarty;
$smarty->caching = 1;
$smarty->display(“news.tpl”);
?>
启用缓存后,调用 display() 和 fetch() 方法在指定模板 (由$cache_dir 属性指定) 中保存目标模板的内容。
处理缓存生命期
缓存的页面在由 $cache_lifetime 属性指定的生命期(以秒为单位)内有效,默认为3600秒,即1小时。因此,如果希望修改此设置,就可以设置这个属性,如下:
<?php
require(“Smarty.class.php”);
$smarty = new Smarty;
$smarty->caching = 1;
//设置生命周期
$smarty->cache_lifetime = 1800;
$smarty->display(“news.tpl”);
?>
在此对象的生命期内,后续调用和缓存的模板都使用此生命期。
有可能需要覆盖以前设置的缓存生命期,从而能分别控制每个模板的缓存生命期。通过将$caching 属性设置为2就可以做到这一点,如下:
<?php
require(“Smarty.class.php”);
$smarty = new Smarty;
$smarty->caching = 2;
$smarty->cache_lifetime = 1200;
$smarty->display(“news.tpl”);
?>
在这里,news.tpl 模板的生命期设置为20分钟,它覆盖了前面设置的全局生命期值。
通过 is_cached( ) 消除处理开销
如前面所述,缓存模板还能消除处理开销,如果禁用缓存(只启用编译),这些处理开销总是会发生。但是,默认情况下并没有启用缓存。要启用缓存,需要把处理指令放在 if 条件中,并执行 is_cached( )方法,如下:
<?php
require(“Smarty.class.php”);
$smarty = new Smarty;
$smarty->caching = 1;

if (! $smarty->is_cached(“news.tpl”)){
$conn = mysql_connect(“localhost”,”name”,”pwd”);
$db = mysql_select_db(“news”);
$query = “select * from news”;
……
}
$smarty->display(“news.tpl”);
?>
在这个例子中,将首先验证模板news.tpl是否有效。如果有效,则跳过数据库访问,否则才访问数据库。
为每个模板创建多个缓存
任何指定的Smarty模板都可以用于为整个新闻项,博客项等提供一个通用界面。由于同一个模板用来生成不同数量的不同项,那么如何缓存一个模板的多个实 例呢?答案比你想像的要简单。Smarty的开发人员实际不已经解决了这个问题,可以通过display()方法为缓存模板的每个实例指派一个唯一标识 符。例如,假设有一个用生成用户信息的模板,并希望缓存这个模板的各个实例:
<?php
require(“Smarty.class.php”);
require(“User.class.php”);

$smarty = new Smarty;
$smarty->caching = 1;

//根据不同的用户ID来区分不同的用户实例来判断有没有被缓存
if(! is_cached(“userinfo.tpl”,$_GET['userid'])){
$user = new User();

$smarty->assign(“name”,$user->getName());
$smarty->assign(“address”,$user->getAddress());
}

/*
当显示时也根据该用户的ID来区分将哪个实例进行缓存,而不影响其它用户的缓存
即是用userid 值来区分同一个缓存模板的不同实例,所有用户都共用一个模板,
但信息都不尽相同,所以不能统一缓存,要独立分开缓存
*/
$smarty->display(“userinfo.tpl”,$_GET['userid']);
?>
特别注意下面一行:
$smarty->display(“userinfo.tpl”,$_GET['userid']);
这一行对于此脚本有两个功能,一方面获取名为$_GET['userinfo'] 的 userinfo.tpl 缓存版本,另一方面,如果还不存在这个缓存,则用这个名字来缓存该模板实例。采用这种方式,可以轻松地为指定模板缓存任意数量的实例。
关于缓存的结语
模板缓存大大提升了应用程序的性能,如果决定将Smarty集成到工程中来,就应当认真地考虑缓存。但是,因为大多数强大的Web应用程序功能都体现在其 动态性上,所以一方面要考虑到性能提升,另一方面也要考虑到缓存页面随时间是否仍有效,要在这二者之间进行权衡

页面: 1 2

类别PHP 标签
  1. nhl pittsburgh penguins jerseys
    发表于 2010年06月13号 17时12分40秒 | 1楼

    Haha! Cute post. Very interesting…

  2. nike basketball shoes
    发表于 2010年06月13号 17时32分07秒 | 2楼

    I was just scanning this posting and was reminded of what I once read.

  3. mike jenkins dallas jersey
    发表于 2010年06月17号 11时57分23秒 | 3楼

    Hmmmm… I think he answers this question clearly!

  4. LV handbags
    发表于 2010年06月18号 06时45分31秒 | 4楼

    Thanks online editor. We love it.

  5. rolex oyster watch
    发表于 2010年06月20号 08时58分25秒 | 5楼

    I complitely agree with you..Thank you for bringing it up!

  6. jimmy choo handbags
    发表于 2010年06月22号 17时17分43秒 | 6楼

    beautiful story… It sounds a bit like my life.

  7. mbt sport shoes
    发表于 2010年06月24号 04时02分28秒 | 7楼

    I love your show when the mind remind us what is precious.

  8. paul smith shirt
    发表于 2010年06月25号 05时02分42秒 | 8楼

    These words mean a great deal to me today. Truly, it is necessary to let go – I see that.

  9. 6 inch timberland boots
    发表于 2010年07月01号 14时55分18秒 | 9楼

    I was thinking today that being grateful every moment is great freedom.

  10. discount herve leger
    发表于 2010年07月08号 06时48分31秒 | 10楼

    well and truly spoken…

  11. black Herve Leger
    发表于 2010年07月11号 01时44分33秒 | 11楼

    Thankyou for sharing.

  12. air force 1 low shoes
    发表于 2010年07月16号 12时11分46秒 | 12楼

    I guess that was my intial thought too.

  13. Herve Leger Sale
    发表于 2010年07月19号 18时50分49秒 | 13楼

    yes, very wise

  14. timberland chukka boot
    发表于 2010年07月19号 20时06分16秒 | 14楼

    I agree wholeheartedly.

  15. miu miu handbag sale
    发表于 2010年07月19号 22时36分24秒 | 15楼

    Thank you, enjoyed the stories, your comments and pictures very, very much.

评论页数:
1 2 159
  1. 目前没有通告
你必需 登陆 才能发表评论.