设为首页 收藏本站
查看: 1182|回复: 0

[经验分享] 30分钟速通,PHP模板引擎Smarty

[复制链接]

尚未签到

发表于 2017-4-8 09:49:25 | 显示全部楼层 |阅读模式
  30分钟速通,PHP模板引擎Smarty
分类: 技术文档 PHP+MySql 标签:

以下内容是是PHP100的PHP教程视频第27,28,29,30讲的浓缩,并添加一些smarty中文手册的补充材料,简练明了,如果看不懂的话。。。还是先看下27-30讲的视频较好。
 
smarty配置:
 
include("smarty/smarty.class.php"); //调用smarty配置文件,默认在smarty根目录下。
$smarty = new smarty(); //新建一个对象
$smarty->caching = false; //关闭缓存,有利于测试。如需开启缓存,改为true
$smarty->cache_lifetime = 60 //设置缓存存活时间,单位秒,必须把caching=true下才作用
$smarty->config_dir = "./configs"; //设置配置文件目录,可用$smarty->config_load()方法来调用配置文件
$smarty->template_dir = "./templates"; //设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录
$smarty->cache_dir = "./caches"; //设置缓存目录
$smarty->left_delimiter = "{"; //缓存左边界符
$smarty->right_delimiter = "}"; //缓存右边界符 
 
smarty应用:
 
$smarty->assign("模板变量","值/数组");
$smarty->display("模板名称");
例:
index.php的代码: 
$value = "bluesdog say : learn smarty only 30 minutes" 
$smarty->assign("content",$value); //进行模板变量替换 
$smarty->display("index.htm") //该文件就是模板文件,应该在./templates模板目录下 

index.htm的代码: 
<HTML>
{if $content ne ""} 
{$content} 
{/if} 
</HTML> 


  • 以上类似:$smarty->cache_lifetime = n 都是smarty的变量
  • 以上类似:$smarty->display(template name) 都是smarty的方法
  • 以上类似:{if $content ne ""}…{/if} 都是smarty的函数
 
 
smarty循环:

smarty共有2种循环函数,section循环多维数组,foreach循环一维简单数组。 
section举例(参数name和loop必不可少,name=自定义循环名 loop=用来循环的变量):
{section name=s loop=$stu}
{$stu.name}
{sectionelse}
无内容
{/section}
例:新闻列表循环
index.php代码:
include("smarty_inc.php"); //smarty配置文件
$news[]=array("title"=>"新闻标题第一条","date"=>"2009-01-01");
$news[]=array("title"=>"新闻标题第二条","date"=>"2009-01-02");
$news[]=array("title"=>"新闻标题第三条","date"=>"2009-01-03");
$news[]=array("title"=>"新闻标题第四条","date"=>"2009-01-04");
$news[]=array("title"=>"新闻标题第五条","date"=>"2009-01-05");
$row=array("标题","作者","当前页");
$smarty->assign("row",$row);
$smarty->assign("news",$news);
$smarty->display("index.htm");
index.htm代码:
<html>
{$row[0]} | {$row[1]} | {$row[2]}
<hr>
<ul>
{section name=list loop=$news}
<li>
{$news
    .title}-{$news
      .date}
      </li>
      {/section}
      </ul>
      </html>
      foreach举例(参数from和item必不可少,item=自定义循环名 from=用来循环的变量):
      //无键值数组
      {foreach from=$name item=id}
      id:{$id}<br>
      </foreach>
      //有键值数组
      {foreach key=j item=v from=$name}
      {$j}:{$v}<br>
      {/foreachelse}
      没有内容了
      {/foreach}
      例:
      include(“smarty_inc.php”); //smarty配置文件 
      $value=array(1,3,5,7);
      $value2=array(‘a’=>’php’,’b’=>’java’,’c’=>’C++);
      $smarty->assign(‘name’,$value);
      $smarty->assign(‘name2’$value2);
      $smarty->display(“index.html”);
      index.html代码:
      {foreach from=$name item=id}
      数组:{$id}<br>
      {/foreach}
      {foreach from=$name item=id key=k}
      数组键值:{$k} 数组内容:{$id}<br>
      {/foreach}

       
       
      smarty内置函数:
       
      (注:这些内置函数都是写在模板文件上的,如index.html,index.tpl。foreach和section也是smarty的内置函数,由于较为重要,所以在上一段单独列出)
       
      include多功能使用
      {include file="header.html"}
      {include file="D:\www\head.htm"} // 可以使用绝对路径
      {include file="head.html" title="bluesdog"} // 如果head.html中有{$title},则直接替换成bluesdog
       
      IF条件语句的使用
      {if $name==’ok’}

      {else}

      {/if}
       
      literal用以忽略页面中的javascript的{}
      {literal}
      <script language=javascript>
      fun1(){
      … …
      }
      </script>
      {/literal}
       
      strip 删除代码中的首尾空格和回车,让代码成一行
      {strip}
      一般放在html代码的开始和结束
      {/strip}
       
       
      smarty缓存:
       
      smarty缓存的配置
      $smarty->cache_dir = "/caches/";  //缓存目录
      $smarty->caching = true;           //开启缓存
      $smarty->cache_lifetime = 60;     //缓存时间,单位秒
      //你可以给不同的模板设置不同的缓存过期时间,一个lifetime,一个display。
       
      smarty缓存的使用和清除
      $smarty->display(‘cache.htm’,cache_id); //创建带ID的缓存 
      $smarty->clear_all_cache(); //清除所有缓存 
      $smarty->clear_cache(‘index.htm’); //清除index.htm的缓存 
      $smarty->clear_cache(‘index.htm’,cache_id); //清除指定id的缓存
       
      smarty局部缓存函数
      (注:insert和blockname都是smarty的内置函数,由于较为重要单独拿出来讲)

      • insert 函数用于小块不缓存内容
        例1:
        index.php
        function insert_shijian (){
         return date("Y-m-d H:m:s");
        }

        index.htm
        <div>{insert name="shijian"}</div>


        例2:
        include(“smarty_inc.php”); //配置的一个smarty类文件 
        include(“mysql_inc.php”); //一个操作mysql的类文件 
        if($_GET[id]){
        $sql=”select * from bluesdog where id=”.$_GET[id];
        $query=$db->query($sql); //mysql类中的方法query=mysql_query() 
        $row=$db->fetch_array($query); 
        $db->query(‘update bluesdog set hit=hit+1 where id=’.$_GET[id]);
        }
        $smarty->assign(‘row’,$row);
        function insert_hits(){
        global $row;
        return $row[2];
        }
        $smarty->display(‘index.html’);
        index.html
        标题:{$row[name]}<br>
        点击:{$row[hit]}次<br> //缓存开启时,这部分内容不会即时更新 
        点击:{insert name=’hits’} //insert就能即时更新了
      • smarty_block 函数用于大量不缓存的内容
        {blockname}
        没有缓存的:{$smarty.now}
        {/blockname}
       
       
      smarty两个最重要的方法:
       

      display显示模板
      display (template [,cache_id [,compile_id]]) 
      cache_id指定一个缓存号。
      compile_id指定一个编译号。
      例:
      include("Smarty.class.php");
      $smarty = new Smarty;
      $smarty->caching = true;
      // is_cached也是smarty的方法之一,用于只有在缓存不存在时才调用数据库
      if(!$smarty->is_cached("index.tpl")){
      $address = "245 N 50th";
      $smarty->assign("Name","Fred");
      $smarty->assign("Address",$address); 
      }
      // 显示输出
      $smarty->display("index.tpl");
       
      fetch 取得输出的内容
      fetch (template [,cache_id [,compile_id]]) 
      返回一个模板输出的内容(HTML代码),而不是像display()那样直接显示出来,常用于静态输出html文件。
      例:
      include("Smarty.class.php");
      $smarty = new Smarty;
      $smarty->caching = true;
      // 只有在缓存不存在时才调用数据库
      if(!$smarty->is_cached("index.tpl")){
       // dummy up some data 
      $address = "245 N 50th"; 
      $db_data = array(  "City" => "Lincoln",  "State" => "Nebraska",  "Zip" = > "68502"  );
      $smarty->assign("Name","Fred"); 
      $smarty->assign("Address",$address); 
      $smarty->assign($db_data);
      }
      // 捕获输出
      $content = $smarty->fetch("index.tpl");
      //下面将内容写入至一个静态文件 
      $file = fopen("index.html","w");
      //OK, 到这里这个index.html静态页就生成了
      fwrite($file,$content);
      $fclose($file);
       
      smarty变量操作符:
      另外smarty一共有20个变量操作符,个人认为只有处理一些英文文章时才有它们的用武之地,所以不再介绍。
      最后,可以用{* 这是一个注释 *} 给smarty的模板文件加上必要的注释
      Smarty常用功能介绍大致就这些,是不是很简单?看看秒表,可能30分钟还没到。
       
      http://www.bluesdog.cn/blog/articles/2010-1/2010181622083.asp

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-361797-1-1.html 上篇帖子: PHP经典面试题(基础型I)附答案 下篇帖子: PHP中插件机制的一种实现方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表