- include_once("smarty/Smarty.class.php");
- $smarty=new Smarty();
- $smarty->config_dir="smarty/Config_File.class.php";
- $smarty->caching=false;
- $smarty->template_dir="./templates";
- $smarty->compile_dir="./templates_c";
- $smarty->cache_dir="./smarty_cache";
- $smarty->left_delimiter="{";
- $smarty->right_delimiter="}";
- ?>
3、编写php文件
index.php
- include("smarty_inc.php");
- $name[]=array("name"=>"新闻第一条","date"=>"2008-1-1");
- $name[]=array("name"=>"新闻第二条","date"=>"2008-2-1");
- $name[]=array("name"=>"新闻第三条","date"=>"2008-3-1");
- $row=array("标题","作者");
- $smarty->assign("title",$name);
- $smarty->assign("row",$row);
- $smarty->display("index.html")
- ?>
4、在templates模板文件夹中编写index.html
index.html
- {$row[0]}|{$row[1]}
- {section name=list loop=$title}
- {$title[list].name}--{$title[list].date}
- {/section}
-
5、使用变量操作符
index.php
- include("smarty_inc.php");
- $value="it is Work and, it Is video";
- $smarty->assign("name",$value);
- $smarty->display("index.html")
- ?>
-
index.html
- 原始内容 : {$name}
- {$name|capitalize}
- {$name|cat:"演示"}
- {$smarty.now|date_format:'%Y-%M-%D'};
- {$name|repalce:"is":"***"};
- {$name|truncate}
6、foreach
index.php
- include("smarty_inc.php");
- $value=array(4,5,6,7);
- $value_key=array('a'=>"PHP",'b'=>"JAVA",'c'=>"C++");
- $smarty->assign("name",$value);
- $smarty->assign("name_key",$value_key);
- $smarty->display("index.html")
- ?>
-
index.html
- {include file="header.html"}
- {foreach from=$name item=id}
- 数组内容: {$id}
- {/foreach}
- {foreach from=$name item=id key=k}
- 数组内容: {$k}--{$id}
- {/foreach}
7、literal
当出现大括号,如使用javascript,可以用literal进行文本处理
8、strip
优化页面,使标签中的空格去掉。使 人不轻易盗用
9、缓存
基本配置:
- include_once("smarty/Smarty.class.php");
- $smarty=new Smarty();
- $smarty->config_dir="smarty/Config_File.class.php";
- $smarty->caching=true;
- $smarty->template_dir="./templates";
- $smarty->compile_dir="./templates_c";
- $smarty->cache_dir="./smarty_cache";
- $smarty->cache_lifetime=60;
- $smarty->left_delimiter="{";
- $smarty->right_delimiter="}";
- ?>
带ID的缓存
- include("smarty_inc.php");
- $id=$_GET[id];
- $value=array(4,5,6,7);
- $smarty->assign("name",$value);
- $smarty->assign("id",$id);
- $smarty->display("index.html",$id);
- ?>
清除缓存和局部缓存
- include("smarty_inc.php");
- $id=$_GET[id];
- $value=array(4,5,6,7);
- //使用insert的局部缓存:
- function insert_shijian(){
- return date("Y-m-d H:m:s");
- }
- $smarty->assign("name",$value);
- $smarty->assign("id",$id);
- $smarty->display("index.html",$id);
- //删除带ID的缓存$smarty->clear_cache('index.html',$id);
- //删除全部缓存$smarty->clear_all_cache();
- ?>
- {insert name='shijian'}
|