首頁 >後端開發 >php教程 >有關smarty的基本設置

有關smarty的基本設置

WBOY
WBOY原創
2016-07-25 09:10:261026瀏覽
複製程式碼
  1. include_once("smarty/Smarty.class.php");
  2. $smarty=new Smarty();
  3. $smarty ->config_dir="smarty/Config_File.class.php";
  4. $smarty->caching=false;
  5. $smarty->template_dir="./templates";
  6. $smarty->compile_dir=" ./templates_c";
  7. $smarty->cache_dir="./smarty_cache";
  8. $smarty->left_delimiter="{";
  9. $smarty->right_delimiter="}";
  10. ? >
複製程式碼

3、寫php文件 index.php

  1. include("smarty_inc.php");
  2. $name[]=array("name"=>"新聞第一條","date"=>"2008-1-1");
  3. $name[]=array("name"=>"新聞第二條","date"=>"2008-2 -1");
  4. $name[]=array("name"=>"新聞第三條","date"=>"2008-3-1");
  5. $row=array("標題","作者");
  6. $smarty->assign("title",$name);
  7. $smarty->assign("row",$row);
  8. $smarty->display ("index.html")
  9. ?>
複製程式碼

4、在templates範本資料夾中撰寫index.html index.html

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

  4. {/section}
複製程式碼

5.使用變數操作符 index.php

  1. include("smarty_inc.php");
  2. $value="it is Work and, it Is video";
  3. $smarty->assign("name",$value);
  4. $smarty->display("index.html")
  5. ?>
複製代碼

index.html

  1. 原文: {$name}
  2. {$name|capitalize}
  3. { $name|cat:"演示"}
  4. {$smarty.now|date_format:'%Y-%M-%D'};
  5. {$name|repalce:"is":"* **"};
  6. {$name|truncate}
複製程式碼

6、foreach index.php

  1. include("smarty_inc.php");
  2. $value=array(4,5,6,7);
  3. $value_key=array('a'=>"PHP",'b'=>"JAVA",'c'=>"C++");
  4. $smarty->assign("name",$ value);
  5. $smarty->assign("name_key",$value_key);
  6. $smarty->display("index.html")
  7. ?>
複製程式碼

index.html

  1. {include file="header.html"}
  2. {foreach from=$name item=id}
  3. 陣列內容: {$id}
  4. {/foreach}
  5. {foreach from=$name item=id key=k}
  6. 陣列內容: {$k}--{$id}
  7. {/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. ? >
複製程式碼
{insert name='shijian'}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn