-
-
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();
- ? >
-
複製程式碼
|
複製程式碼