Chinaunix首页 | 论坛 | 博客
  • 博客访问: 326706
  • 博文数量: 206
  • 博客积分: 1040
  • 博客等级: 少尉
  • 技术积分: 1756
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 17:22
文章分类

全部博文(206)

文章存档

2015年(3)

2014年(147)

2013年(2)

2012年(54)

我的朋友

分类: 系统运维

2012-06-02 13:07:03

1、将lib文件夹导入项目中,改名字为smarty
2、编写配置程序,并建立相应的模板、编译、缓存文件夹

点击(此处)折叠或打开

  1. smarty_inc.php
  2. include_once("smarty/Smarty.class.php");
  3. $smarty=new Smarty();
  4. $smarty->config_dir="smarty/Config_File.class.php";
  5. $smarty->caching=false;
  6. $smarty->template_dir="./templates";
  7. $smarty->compile_dir="./templates_c";
  8. $smarty->cache_dir="./smarty_cache";
  9. $smarty->left_delimiter="{";
  10. $smarty->right_delimiter="}";
  11. ?>
3、编写php文件

点击(此处)折叠或打开

  1. index.php
  2. include("smarty_inc.php");
  3. $name[]=array("name"=>"新闻第一条","date"=>"2008-1-1");
  4. $name[]=array("name"=>"新闻第二条","date"=>"2008-2-1");
  5. $name[]=array("name"=>"新闻第三条","date"=>"2008-3-1");
  6. $row=array("标题","作者");
  7. $smarty->assign("title",$name);
  8. $smarty->assign("row",$row);
  9. $smarty->display("index.html")
  10. ?>
4、在templates模板文件夹中编写index.html

点击(此处)折叠或打开

  1. index.html
  2. {$row[0]}|{$row[1]}
  3. {section name=list loop=$title}
  4. {$title[list].name}--{$title[list].date}

  5. {/section}

5、使用变量操作符

点击(此处)折叠或打开

  1. index.php
  2. include("smarty_inc.php");
  3. $value="it is Work and, it Is video";
  4. $smarty->assign("name",$value);
  5. $smarty->display("index.html")
  6. ?>
  7. index.html
  8. 原始内容 : {$name}
  9. {$name|capitalize}
  10. {$name|cat:"演示"}
  11. {$smarty.now|date_format:'%Y-%M-%D'};
  12. {$name|repalce:"is":"***"};
  13. {$name|truncate}

6、foreach


点击(此处)折叠或打开

  1. index.php
  2. include("smarty_inc.php");
  3. $value=array(4,5,6,7);
  4. $value_key=array('a'=>"PHP",'b'=>"JAVA",'c'=>"C++");
  5. $smarty->assign("name",$value);
  6. $smarty->assign("name_key",$value_key);
  7. $smarty->display("index.html")
  8. ?>
  9. index.html
  10. {include file="header.html"}
  11. {foreach from=$name item=id}
  12. 数组内容: {$id}
  13. {/foreach}
  14. {foreach from=$name item=id key=k}
  15. 数组内容: {$k}--{$id}
  16. {/foreach}
7、literal
当出现大括号,如使用javascript,可以用literal进行文本处理

8、strip
优化页面,使标签中的空格去掉。使 人不轻易盗用
 
9、缓存
基本配置:

点击(此处)折叠或打开

  1. include_once("smarty/Smarty.class.php");
  2. $smarty=new Smarty();
  3. $smarty->config_dir="smarty/Config_File.class.php";
  4. $smarty->caching=true;
  5. $smarty->template_dir="./templates";
  6. $smarty->compile_dir="./templates_c";
  7. $smarty->cache_dir="./smarty_cache";
  8. $smarty->cache_lifetime=60;
  9. $smarty->left_delimiter="{";
  10. $smarty->right_delimiter="}";
  11. ?>
带ID的缓存

点击(此处)折叠或打开

  1. include("smarty_inc.php");
  2. $id=$_GET[id];
  3. $value=array(4,5,6,7);
  4. $smarty->assign("name",$value);
  5. $smarty->assign("id",$id);
  6. $smarty->display("index.html",$id);
  7. ?>
清除缓存和局部缓存

点击(此处)折叠或打开

  1. include("smarty_inc.php");
  2. $id=$_GET[id];
  3. $value=array(4,5,6,7);
  4. //使用insert的局部缓存:
  5. function insert_shijian(){
  6. return date("Y-m-d H:m:s");
  7. }
  8. $smarty->assign("name",$value);
  9. $smarty->assign("id",$id);
  10. $smarty->display("index.html",$id);
  11. //删除带ID的缓存$smarty->clear_cache('index.html',$id);
  12. //删除全部缓存$smarty->clear_all_cache();
  13. ?>
  14. {insert name='shijian'}



 
阅读(1664) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~