欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

有关smarty的基本设置

程序员文章站 2022-04-06 23:00:31
...
  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'}
复制代码