• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

30分钟速通,PHP模板发动机Smarty

php 搞代码 3年前 (2022-01-24) 26次浏览 已收录 0个评论

30分钟速通,PHP模板引擎Smarty

30分钟速通,PHP模板引擎Smarty

  • 以上类似:$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[s].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代码:

{$row[0]} | {$row[1]} | {$row[2]}




    {section name=list loop=$news}


  • {$news
      .title}-{$news
        .date}

      • {/section}


      foreach举例(参数from和item必不可少,item=自定义循环名 from=用来循环的变量):
      //无键值数组
      {foreach from=$name item=id}
      id:{$id}

      //有键值数组
      {foreach key=j item=v from=$name}
      {$j}:{$v}

      {/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}

      {/foreach}
      {foreach from=$name item=id key=k}
      数组键值:{$k} 数组内容:{$id}

      {/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_cach*本文来源gaodai#ma#com搞@代~码^网+搞代gaodaima码e(); //清除所有缓存?
      $smarty->clear_cache(‘index.htm’); //清除index.htm的缓存?
      $smarty->clear_cache(‘index.htm’,cache_id); //清除指定id的缓存

      ?

      smarty局部缓存函数
      (注:insert和blockname都是smarty的内置函数,由于较为重要单独拿出来讲)

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

        index.htm
        {insert name=”shijian”}




        例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]}

        点击:{$row[hit]}次
        //缓存开启时,这部分内容不会即时更新?
        点击:{insert name=’hits’} //insert就能即时更新了


      2. 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


      搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
      转载请注明原文链接:30分钟速通,PHP模板发动机Smarty
      喜欢 (0)
      [搞代码]
      分享 (0)
      发表我的评论
      取消评论

      表情 贴图 加粗 删除线 居中 斜体 签到

      Hi,您需要填写昵称和邮箱!

      • 昵称 (必填)
      • 邮箱 (必填)
      • 网址