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:
指明在查找模板变量时的左右分割符。默认情况下为{与},但在实际中因为我们要在模板中使用
==========================================
example6.php
==========================================
templates(./templates); //一般不要,要了出错.自己实验得出
$smarty->templates_c(./templates_c); //一般不要,要了出错.自己实验得
$smarty->cache(./cache); //一般不要,要了出错.自己实验得
$smarty->cache_lifetime = 0; //一般不要,要了出错.自己实验得
$smarty->caching = true; //一般不要,要了出错.自己实验得
$smarty->left_delimiter = $smarty->right_delimiter = }>;
$array[] = array(newsid=>1, newstitle=>第1条新闻);
$array[] = array(newsid=>2, newstitle=>第2条新闻);
$array[] = array(newsid=>3, newstitle=>第3条新闻);
$array[] = array(newsid=>4, newstitle=>第4条新闻);
$array[] = array(newsid=>5, newstitle=>第5条新闻);
$array[] = array(newsid=>6, newstitle=>第6条新闻);
$smarty->assign(newsarray, $array);
//编译并显示位于./templates下的index.tpl模板
$smarty->display(example6.tpl);
?>
以上就介绍了shavarsh karapetyan smarty 原来也不过如此~~呵呵,包括了shavarsh karapetyan方面的内容,希望对php教程有兴趣的朋友有所帮助。