caching=false;$smarty->template_dir="./templates";$smarty->compile_di">
当前位置:Gxlcms > PHP教程 > 有关smarty的基本设置

有关smarty的基本设置

时间:2021-07-01 10:21:17 帮助过:25人阅读

  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. ?>
  1. {insert name='shijian'}

人气教程排行