频道栏目
首页 > 资讯 > Smarty > 正文

Smarty 入门教程

16-01-08        来源:[db:作者]  
收藏   我要投稿

序言 
区域内容 

 这些区块大部份都会在 PHP 程式中以 if 或 for, while 来控制它们的显示状态,虽然样版看起来简洁多了,但只要一换了显示方式不同的样版, PHP 程式势必要再改一次!main.php:

php代码:

   include "class/Smarty.class.php";
   define('__SITE_ROOT', 'd:/appserv/web/demo'); // 最后没有斜线
   $tpl = new Smarty();
   $tpl->template_dir = __SITE_ROOT . "/templates/";
   $tpl->compile_dir = __SITE_ROOT . "/templates_c/";
   $tpl->config_dir = __SITE_ROOT . "/configs/";
   $tpl->cache_dir = __SITE_ROOT . "/cache/";
   $tpl->left_delimiter = '<{';
   $tpl->right_delimiter = '}>';
?>

 

 照上面方式设定的用意在于,程式如果要移植到其他地方,只要改 __SITE_ROOT 就可以啦。 (这裡是参考 XOOPS 的 )templates/test.htm:

 

php代码:

<{$content}>

现在我们要将上面的样版显示出来,并将网页标题 ($title) 与内容 ($content) 更换,请将以下档桉内容命名为 test.php ,并放置在主资料夹下:test.php:

php代码:

   require "main.php";
   $tpl->assign("title", "测试用的网页标题");
   $tpl->assign("content", "测试用的网页内容");
   // 上面两行也可以用这行代替
   // $tpl->assign(array("title" => "测试用的网页标题", "content" => "测试用的网页内容"));
   $tpl->display('test.htm');
?>templates_c/%%179/%%1798044067/test.htm.php:


_tpl_vars['content']; ?>

没错,这就是 Smarty 编译过的档桉。它将我们在样版中的变数转换成了 PHP 的语法来执行,下次再读取同样的内容时, Smarty 就会直接抓取这个档桉来执行了。main.php:

php代码:

   include "class/Smarty.class.php";
   define( '__SITE_ROOT', 'd:/appserv/web/demo'); // 最后没有斜线
   // 以 main.php 的位置为基准
   require_once "includes/functions.php";
   require_once "includes/include.php";
   $tpl = new Smarty();
   $tpl- >template_dir = __SITE_ROOT . "/templates/";
   $tpl- >compile_dir = __SITE_ROOT . "/templates_c/";
   $tpl- >config_dir = __SITE_ROOT . "/configs/";
   $tpl- >cache_dir = __SITE_ROOT . "/cache/";
   $tpl- >left_delimiter = '<{';
   $tpl- >right_delimiter = '}>';
?>

 

modules 这个资料夹则是用来放置程式模组的,如此一来便不会把程式丢得到处都是,整体架构一目瞭然。

1. <{$var}>

  2. <{ $var }>

  3. <{$var}>

  在 Smarty 裡,变数预设是全域的,也就是说你只要指定一次就好了。指定两次以上的话,变数内容会以最后指定的为主。就算我们在主样版中载入了外部的子样版,子样版中同样的变数一样也会被替代,这样我们就不用再针对子样版再做一次解析的动作。<{变数|修饰函式}>

  <{变数|修饰函式:"参数(非必要,视函式而定)"}>   

  范例如下:

php代码:

<{$var|nl2br}>
<{$var|string_format:"%02d"}>

总金额:21,000 元  

 一般样版引擎的样版可能会这样写:

  总金额:{format_total} 元 
php代码:

   $total = 21000;
   $tpl->assign("total", $total);
   $tpl->assign("format_total", number_format($total));
?>

而 Smarty 的样版就可以这样写: (number_format 修饰函式请到 Smarty 官方网页下载)

总金额:<{$total|number_format:""}> 元

php代码:

   $total = 21000;
   $tpl->assign("total", $total);
?>

 所以在 Smarty 中我们只要指定一次变数,剩下的交给样版自行决定即可。这样瞭解了吗?这就是让样版自行决定变数呈现风貌的好处!

控制样版的内容

重覆的区块 test2.php:

php代码:

   require "main.php";
   $array1 = array(1 => "苹果", 2 => "凤梨", 3 => "香蕉", 4 => "芭乐");
   $tpl->assign("array1", $array1);
   $array2 = array(
   array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"),
   array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"),
   array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"),
   array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"),
   array("index1" => "data5-1", "index2" => "data5-2", "index3" => "data5-3"));
   $tpl->assign("array2", $array2);
   $tpl->display("test2.htm");
?>

而样版的写法如下:templates/test2.htm:

php代码:

利用 foreach 来呈现 array1 <{foreach item=item1 from=$array1}> <{$item1}> <{/foreach}> 利用 section 来呈现 array1 <{section name=sec1 loop=$array1}> <{$array1[sec1]}> <{/section}> 利用 foreach 来呈现 array2 <{foreach item=index2 from=$array2}> <{foreach key=key2 item=item2 from=$index2}> <{$key2}>: <{$item2}> <{/foreach}> <{/foreach}> 利用 section 来呈现 array1 <{section name=sec2 loop=$array2}> index1: <{$array2[sec2].index1}> index2: <{$array2[sec2].index2}> index3: <{$array2[sec2].index3}> <{/section}>

test3.php:   require "main.php";
   $forum = array(
     array("category_id" => 1, "category_name" => "公告区",
       "topic" => array(
         array("topic_id" => 1, "topic_name" => "站务公告")
       )
     ),
     array("category_id" => 2, "category_name" => "文学专区",
       "topic" => array(
         array("topic_id" => 2, "topic_name" => "好书介绍"),
         array("topic_id" => 3, "topic_name" => "奇文共赏")
       )
     ),
     array("category_id" => 3, "category_name" => "电脑专区",
       "topic" => array(
         array("topic_id" => 4, "topic_name" => "硬体週边"),
         array("topic_id" => 5, "topic_name" => "软体讨论")
       )
     )
   );
   $tpl->assign("forum", $forum);
   $tpl->display("test3.htm");
?>

  样版的写法如下:templates/test3.htm:

php代码:


 <{section name=sec1 loop=$forum}>
  
    
  
   <{section name=sec2 loop=$forum[sec1].topic}>
  
    
    
  
   <{/section}>
   <{/section}>
<{$forum[sec1].category_name}>
  <{$forum[sec1].topic[sec2].topic_name}>



test3.php:

 

php代码:

   require "main.php";
   // 先建立第一层阵列
   $category = array();
   $db->setSQL($SQL1, 'CATEGORY');
   if (!$db->query('CATEGORY')) die($db->error());
   // 抓取第一层迴圈的资料
   while ($item_category = $db->fetchAssoc('CATEGORY'))
   {
     // 建立第二层阵列
     $topic = array();
     $db->setSQL(sprintf($SQL2, $item_category['category_id']), 'TOPIC');
     if (!$db->query('TOPIC')) die($db->error());
     // 抓取第二层迴圈的资料
     while ($item_topic = $db->fetchAssoc('TOPIC'))
     {
       // 把抓取的资料推入第二层阵列中
       array_push($topic, $item_topic);
     }
     // 把第二层阵列指定为第一层阵列所抓取的资料中的一个成员
     $item_category['topic'] = $topic;
     // 把第一层资料推入第一层阵列中
     array_push($category, $item_category);
   }
   $tpl->assign("forum", $category);
   $tpl->display("test3.htm");
?>
  在资料库抓取一笔资料后,我们得到的是一个包含该笔资料的阵列。透过 while 叙述及 array_push 函式,我们将资料库中的资料一笔一笔塞到阵列裡。如果您只用到单层迴圈,就把第二层迴圈 (红色的部份) 去掉即可。

决定内容是否显示 <{if $is_login == true}>


  显示使用者操作选单

  <{else}>

  显示输入帐号和密码的表单

  <{/if}>

  

  要注意的是,「==」号两边一定要各留至少一个空白字元,否则 Smarty 会无法解析。test4.php:

php代码:

   require "main.php";
   $my_array = array(
     array("value" => "0"),
     array("value" => "1"),
     array("value" => "2"),
     array("value" => "3"),
     array("value" => "4"),
     array("value" => "5"),
     array("value" => "6"),
     array("value" => "7"),
     array("value" => "8"),
     array("value" => "9"));
   $tpl->assign("my_array", $my_array);
   $tpl->display('test4.htm');
?>

  样版的写法如下:templates/test4.htm:

php代码:


   <{section name=sec1 loop=$my_array}>
    
   <{if $smarty.section.sec1.rownum is div by 2}>
  
  
   <{/if}>
   <{/section}>
  
<{$my_array[sec1].value}>


test5.php:   require "main.php";
   $tpl->assign("title", "Include 测试");
   $tpl->assign("content", "这是样版 2 中的变数");
   $tpl->assign("dyn_page", "test5_3.htm");
   $tpl->display('test5_1.htm');
?>

  样版 1 的写法如下:templates/test5_1.htm:

php代码:

<{include file="test5_2.htm"}>

<{include file=$dyn_page}>
<{include file="test5_4.htm" custom_var="自订变数的内容"}>

templates/test5_2.htm:<{$content}>

样版 3 的写法如下:templates/test5_3.htm:这是样版 3 的内容
  templates/test5_4.htm:<{$custom_var}>  

  这裡注意几个重点:1. 样版的位置都是以先前定义的 template_dir 为基准;2. 所有 include 进来的子样版中,其变数也会被解译。;3. include 中可以用「变数名称=变数内容」来指定引含进来的样版中所包含的变数,如同上面样版 4 的做法。

相关TAG标签
上一篇:PHP详细彻底学习Smarty
下一篇:Smarty的缓存操作技巧
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站