Home  >  Article  >  Backend Development  >  smarty 原来也不过如此~~呵呵_php技巧

smarty 原来也不过如此~~呵呵_php技巧

WBOY
WBOYOriginal
2016-05-17 09:43:47772browse

include_once("./comm/Smarty.class.php"); //包含smarty类文件 
$smarty = new Smarty(); //建立smarty实例对象$smarty 
$smarty->templates("./templates"); //设置模板目录 
$smarty->templates_c("./templates_c"); //设置编译目录 
//****大家注意,这里我是我新加入的****// 
$smarty->cache("./cache"); //设置缓存目录 
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间 
$smarty->caching = true; //设置缓存方式 
//---------------------------------------------------- 
//左右边界符,默认为{},但实际应用当中容易与JavaScript 
//相冲突,所以建议设成或其它。 
//---------------------------------------------------- 
$smarty->left_delimiter = "$smarty->right_delimiter = "}>"; 
$smarty->assign("name", "李晓军"); //进行模板变量替换 
//编译并显示位于./templates下的index.tpl模板 
$smarty->display("index.tpl"); 
?>
我们可以看到,smarty的程序部分实际就是符合php语言规范的一组代码,我们依次来解释一下:
1。/**/语句:
包含的部分为程序篇头注释。主要的内容应该为对程序的作用,版权与作者及编写时间做一个简单的介绍,这在smarty中不是必 
需的,但从程序的风格来讲,这是一个好的风格。 
2。include_once语句:
它将安装到网站的smarty文件包含到当前文件中,注意包含的路径一定要写正确。 
3。$smarty = new Smarty():
这一句新建一个Smarty对象$smarty,简单的一个对象的实例化。 
4。$smarty->templates(""):
这一句指明$smarty对象使用tpl模板时的路径,它是一个目录,在没有这一句时,Smarty默认的模板路径为当前目录的templates 
目录,实际在写程序时,我们要将这一句写明,这也是一种好的程序风格。
5。$smarty->templates_c(""):
这一句指明$smarty对象进行编译时的目录。在模板设计篇我们已经知道Smarty是一种编译型模板语言,而这个目录,就是它编译 
模板的目录,这里要注意,如果站点位于*nix服务器上,请确保teamplates_c里定义的这个目录具有可写可读权限,默认情况下它的编译目录 
是当前目录下的templates_c,出于同样的理由我们将其明确的写出来。 
6。$smarty->left_delimiter与$smarty->right_delimiter:
指明在查找模板变量时的左右分割符。默认情况下为"{"与"}",但在实际中因为我们要在模板中使用

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn