!!     -
   
Smarty J
Ǩ

}lIJ˪ PHP ]pvAť Smarty ɡA|ıoC굧̤]ҥ~AII@UCOӦbR XOOPS {[cɡA}lo{ Smarty äCunN Smarty ¦\mnAb@ΤWNwg۷FCM¦ॴnA᭱iΤ]NΩȤFC

og峹DnηNëDn`JQ Smarty ϥΡAobxϥλwggoܧFC̶ȦbgU@ǦۤvϥΤW߱oAQnF Smarty ooӤJB͡AiHqo@DZҥܡCN]og峹eOD``JA|ϥ Smarty B̥ͭi|ıo²FIC

ثewgĤT׭qFAQh[@ǮƶiӡFLêɶYAܦh Smarty iޥ̨èSsoܳzAҥH]XӲ{A̬۫Hog峹ӯjhƷQDz Smarty Ǫ̤FCM妳ջ~a]wiA̷|bU@׭q󥿪C

P.S. Цb 1024 x 768 ҤUݷ|nC

jaceju@seed.net.tw 2004/02/09


  1. Smarty

    1. DqɭԶ}lAH}l HTML OJ Server Script ıoӺNCMӤ׬OLn ASP άO}񷽽X PHPAOݩ󤺴O Server Script AݻyC]]NHQApG{޿ (κٰӷ~޿) Pe{ (Layout) ޿ܡAOO|nOH

      oӰDNsbw[Aqʦ}lɡA׬O ASP άO PHP ϥΪ̳Oݵ{}o̻Pı]pvبCiOq`oǨϥΪ̤O{jNOujApGn̦PɭUAioָӭM...

      ҥH˪NBӥͰաI˪تANOnFWz쪺޿\C{}o̱M`ƪάO\઺FFӵı]pvhiM`ƪAݰ_ӧ㦳M~PI]˪ܾAXq}oζϥΡAϨCӤHoMI

      ṈIJL˪ӻA̸Ƨe{覡jGݷft{Bz˪MѼ˪ۦMw˪اΦC

      bݷft{Bz˪A{}o̥ntdܼƪe{޿A]NOLܼƪebX˪eBznA~వ assign u@CyܻA{}o٬Oohg@ǵ{ӨMwܼƧe{CӧѼ˪ۦMw˪A\ܼƪ assign ˪Aı]pvb]p˪ɦAMwܼƭnpe{C]Ni|t@Mݩۤv˪{yk (p Smarty) AHKܼƪe{Coˤ@ӡAı]pv]oDzߦpϥμ˪yC


    2. ˪B@z
    3. ڭ̥ݬݥHUB@ϡG

      @몺˪ (p PHPLib) Obإ߼˪ɨonѪR˪AMܼƮMJAzL parse() oӤkӸѪR˪A̫ANXC

      Smarty ϥΪ̨ӻA{̤]ݭn parse ʧ@FAo Smarty ۰ʷ|ڭ̰CӥBwgsĶLApG˪SܰʪܡA Smarty N۰ʸLsĶʧ@AsĶLAH`ٽsĶɶC


    4. ϥSmarty@Ƿ
    5. b@˪Aڭ̱`ݨϰ쪺[Aҿװ϶j|oˡG

      <!-- START : Block name -->
      ϰ줺e
      <!-- END : Block name -->

      oǰ϶j|b PHP {H if for, while ӱ̪ܪAAM˪ݰ_²hFAun@Fܤ覡P˪A PHP {եnA@I

      b Smarty A@HܼƬDAҦe{޿賣˪ۦ汱C] Smarty |ۤv˪yAҥHެO϶O_n٬OnСAO Smarty ˪yk (if, foreach, section) ftܼƤe@e{Coˤ@ӷPıWn˪ܱoIAnBOunWoA PHP {@泣C

      ѤWAڭ̥iHDϥ Smarty nx@ӭhGN{޿Pe{޿TaCNO PHP {̤nӦh HTML XC{unMwnܼƭn˪̡A˪ۤvMwӦpe{oܼ (Ʀܤe{]) C


  2. Smarty¦

    1. wSmarty
    2. Aڭ̥Mw{mmC
      WindowsUi|o˪mGu d:\appserv\web\demo\ vC
      LinuxUi|o˪mGu /home/jaceju/public_html/ vC
      SmartyxU̷sSmartyMGhttp://smarty.php.netC
      Ѷ} Smarty 2.6.0 A|ݨܦhɮסA䤤 libs ƧCb libs ӷ| 3 class.php + 1 debug.tpl + 1 plugin Ƨ + 1 core ƧCM᪽N libs ƻsz{DƧUAAW class NiHFCNoˡHSIoئw˪k²AAX@SۤvDϥΪ̡C

      ܩ Smarty xUnФ@Ǥwˤ覡OH򥻤W̷өx誺覡wˡAiHubDwˤ@AMᴣѵӥDUҦ]p̶}oP{ɪޥΡAӤ|ЦwˤӦh Smarty ƥCӵ̩ҴѪ覡hOAXn{aLӲLh{}ǫϥΡAoˤηдoDSw Smarty C


    3. {Ƨ]w
    4. H̦bWindowswAppservҡA{DƧOud:\appserv\web\demo\vCw˦nSmartyAڭ̦bDƧUAإ߳o˪ƧG

      b Linux UAаOoN templates_c vܧ 777 CWindows UhNŪC


    5. Ĥ@ӥSmartygp{
    6. ڭ̥]w Smarty |AбNHUoɮשRW main.php AémDƧUG

      main.php:
      <?php
          include "class/Smarty.class.php";
          define('__SITE_ROOT', 'd:/appserv/web/demo'); // ̫S׽u
          $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 = '}>';
      ?>

      ӤW覡]wηNbA{pGnӨLaAun __SITE_ROOT NiHաC (o̬OѦ XOOPS )

      Smarty ˪|]wnA{|̷ӳoӸ|ӧҦ˪۹m (dҤO 'd:/appserv/web/demo/templates/' ) CMڭ̥ display() o Smarty kܧڭ̪˪C

      Uӧڭ̦b templates ƧUm@ test.htmG(ɦWs򳣵LҿסAKı]pv}oA̳٬OH .htm DC)

      templates/test.htm:
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=big5">
      <title><{$title}></title>
      </head>
      <body>
      <{$content}>
      </body>
      </html>

      {bڭ̭nNW˪ܥXӡAñND ($title) Pe ($content) 󴫡AбNHUɮפeRW test.php AémbDƧUG

      test.php:
      <?php
          require "main.php";
          $tpl->assign("title", "եΪD");
          $tpl->assign("content", "եΪe");
          // W]iHγoN
          // $tpl->assign(array("title" => "եΪD", "content" => "եΪe"));
          $tpl->display('test.htm');
      ?>

      Х}sAJ http://localhost/demo/test.php ոլ(̱zҨMw})Aӷ|ݨHUeG

      A templates_c UAڭ̷|ݨ@ө_ǪƧ (%%179) AAIUh]O@ө_ǪƧ (%%1798044067) AӨ䤤@ɮסG

      templates_c/%%179/%%1798044067/test.htm.php:
      <?php /* Smarty version 2.6.0, created on 2003-12-15 22:19:45 compiled from test.htm */ ?>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=big5">
      <title><?php echo $this->_tpl_vars['title']; ?></title>
      </head>
      <body>
      <?php echo $this->_tpl_vars['content']; ?>
      </body>
      </html>

      SAoNO Smarty sĶLɮסCNڭ̦b˪ܼഫF PHP ykӰAUAŪP˪eɡA Smarty N|oɮרӰFC

      ̫ڭ̾z@U Smarty {gBJG

      Step 1. J Smarty ˪C
      Step 2. إ Smarty C
      Step 3. ]w Smarty 󪺰ѼơC
      Step 4. b{BzܼƫAA Smarty assign kNܼƸmJ˪̡C
      Step 5. Q Smarty display kNqXC


    7. pwƧA{[c
    8. Wڭ̬ݨ찣F Smarty һݭnƧ~ (class B configs B templates B templates_c) A٦ӸƧG includes B modules CoO̼ҥ XOOPS [cҫإߥXӪA] XOOPS O̩ұIJ쪺{Aּƨϥ Smarty ˪[{CҿצʰjA̳o˪{[cS XOOPS ʤ@jAܤֵHݮ٦ XOOPS yC

      includes oӸƧDnOΨөm@ function B sql ɡAo˦b main.php NiHN̤ޤJFApUG

      main.php:
      <?php
          include "class/Smarty.class.php";
          define('__SITE_ROOT', 'd:/appserv/web/demo'); // ̫S׽u
          // H main.php m
          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 oӸƧhOΨөm{ҲժAp@ӫK|{oBOA[c@AMC

      Wڭ̤] main.php AoOӵ{Dn֤ߡA׬O`ƩwqB~{JB@ܼƫإߵAObo̶}lCҥH᪺ҲճunNoɮץ]tiӴNiHաC]b{y{WANnnc main.php ӭn񨺨ǪFFMQ include require OAC`MOAnLFC

      bW`쪺 Smarty { 5 BJA main.php N|ڭ̥Ne 3 ӨBJnA᭱Ҳյ{un᭱ӨBJNiHFC


  3. qܼƶ}l

    1. pϥܼ
    2. qW@dҤAڭ̥iHMaݨڭ̧Q <{ }> oӼХܲŸNܼƥ]_ӡCw]ХܲŸ { } AFĽX Javascript YA]٬Oҥ XOOPS ANХܲŸCܼƪRW覡M PHP ܼƩRW覡O@Ҥ@˪Ae] $ r (oM@몺˪P)CХܲŸNIO PHP <?php ?> (ƹW̪T|Qo) AҥHHU˪ܼƼgkOi檺G

      1. <{$var}>
      2. <{ $var }> <!-- MܼƤŮ -->
      3. <{$var
      }> <!-- ҩlХܲŸMХܲŸbP@ -->

      b Smarty ̡Aܼƹw]O쪺A]NOAunw@NnFCw⦸HWܡAܼƤe|H̫wDCNڭ̦bD˪JF~l˪Al˪P˪ܼƤ@ˤ]|QNAo˧ڭ̴NΦAwl˪A@ѪRʧ@C

      Ӧb PHP {Aڭ̥ Smarty assign ӱNܼƸm˪C assign ΪkxUwggoܦhFAΪkNpPW@`dҩҥܡCLbа϶ɡAڭ̴NNܼư@Ǥ}A~Nܼ assign ˪AobU@AC


    3. ׹Aܼ
    4. Wڭ̴ Smarty ܼƧe{OѼ˪ۦMwAҥH Smarty ѤF\h׹ܼƪ禡CϥΪkpUG

      <{ܼ|׹禡}> <!-- ׹禡SѼƮ -->
      <{ܼ|׹禡:"Ѽ(DnA禡өw)"}> <!-- ׹禡ѼƮ -->

      dҦpUG

      <{$var|nl2br}> <!-- NܼƤr <br /> -->
      <{$var|string_format:"%02d"}> <!-- NܼƮ榡 -->

      nAn˪ۦMwܼƧe{HݬݩU HTML AoOYʪbeC

      <input name="total" type="hidden" value="21000" />
      `BG21,000

      @˪˪i|o˼gG

      <input name="total" type="hidden" value="{total}" />
      `BG{format_total}

      ̪ PHP {no˼gG

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

      Smarty ˪NiHo˼gG (number_format ׹禡Ш Smarty xU)

      <input name="total" type="hidden" value="<{$total}>" />
      `BG<{$total|number_format:""}>

      Smarty PHP {uno˼gG

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

      ҥHb Smarty ڭ̥unw@ܼơAѤU浹˪ۦMwYiCoAѤFܡHoNO˪ۦMwܼƧe{nBI


  4. ˪e

    1. Ъ϶
    2. b Smarty ˪OAڭ̭nФ@Ӱ϶ؤ覡G foreach section CӦb{ڭ̫hn assign @Ӱ}CAoӰ}CiH]tƲհ}CCNUoӨҤlG

      ڭ̨Ӭ PHP {OpgG

      test2.php:
      <?php
          require "main.php";
          $array1 = array(1 => "īG", 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");
      ?>

      Ӽ˪gkpUG

      templates/test2.htm:
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=big5">
      <title>խа϶</title>
      </head>
      <body>
      <pre>
      Q foreach ӧe{ array1
      <{foreach item=item1 from=$array1}>
      <{$item1}>
      <{/foreach}>
      Q section ӧe{ array1
      <{section name=sec1 loop=$array1}>
      <{$array1[sec1]}>
      <{/section}>
      Q foreach ӧe{ array2
      <{foreach item=index2 from=$array2}>
      <{foreach key=key2 item=item2 from=$index2}>
      <{$key2}>: <{$item2}>
      <{/foreach}>
      <{/foreach}>
      Q section ӧe{ array1
      <{section name=sec2 loop=$array2}>
      index1: <{$array2[sec2].index1}>
      index2: <{$array2[sec2].index2}>
      index3: <{$array2[sec2].index3}>
      <{/section}>
      </pre>
      </body>
      </html>

      WҫAڭ̵o{ެO foreach section Ӱ浲GO@˪C̨쩳󤣦POH

      Ĥ@ӮtOܩANO foreach nH_Bz覡ӧe{ڭ̩ assign h}CܼơA section hHuD}C[jW].l}CޡvYiNӰ}Ce{XӡCѦiA Smarty b˪ foreach M PHP foreach O@˪F section hO Smarty FBzpWC}CܼƩҵoiXӪԭzCM section \٤upAFU@`ҽͨ쪺_Ƨe{~AxU]ѤFnX section νdҡC

      Ln`NOAᵹ section }CޥOq 0 }lơAY 0, 1, 2, 3, ...CpGz}CޤOq 0 }lơANo foreach ӧe{zơCziHѦҩxQװϤgQA䤤QF section M foreach ΪkC


    3. _ƪe{
    4. ˪̳̥OH˸jNO_ƪe{aA\hۦW˪|SNjճoIALo Smarty ӻoOpC

      ̱`쪺_ơANε{QץDDϧaC]ne{GpUG

      i
        Ȥi
      DZM
       
        _@
      qM
        wg
        nQ

      {ڭ̥HRAƬҡG

      test3.php:
      <?php
          require "main.php";
          $forum = array(
              array("category_id" => 1, "category_name" => "i",
                  "topic" => array(
                      array("topic_id" => 1, "topic_name" => "Ȥi")
                  )
              ),
              array("category_id" => 2, "category_name" => "DZM",
                  "topic" => array(
                      array("topic_id" => 2, "topic_name" => "nѤ"),
                      array("topic_id" => 3, "topic_name" => "_@")
                  )
              ),
              array("category_id" => 3, "category_name" => "qM",
                  "topic" => array(
                      array("topic_id" => 4, "topic_name" => "wg"),
                      array("topic_id" => 5, "topic_name" => "nQ")
                  )
              )
          );
          $tpl->assign("forum", $forum);
          $tpl->display("test3.htm");
          ?>

      ˪gkpUG

      templates/test3.htm:
      <html>
      <head>
      <title>_j</title>
      </head>
      <body>
      <table width="200" border="0" align="center" cellpadding="3" cellspacing="0">
          <{section name=sec1 loop=$forum}>
          <tr>
              <td colspan="2"><{$forum[sec1].category_name}></td>
          </tr>
          <{section name=sec2 loop=$forum[sec1].topic}>
          <tr>
              <td width="25">&nbsp;</td>
              <td width="164"><{$forum[sec1].topic[sec2].topic_name}></td>
          </tr>
          <{/section}>
          <{/section}>
      </table>
      </body>
      </html>

      檺GN|Ҥl@ˡC

      ]OAb{ڭ̥unQkҭnЭȤ@h@h}CAAQ <{Ĥ@h}C[j1].ĤGh}C[j2].ĤTh}C[j3]. ... .}C}> o˪覡ܨC@ӱ_j餤ȡCܩΤkOHU@`ϥθƮwɧڭ̦AC


    5. ഫƮw
    6. Wpܱ_jAӹڤWήɧڭ̪ƥiOqƮwXӪAҥHڭ̴NoQkƮwܦWzh}CΦCo̵̥Τ@ DB OӧƮwơAziHۦαzwkC

      ڭ̥uק PHP {A˪٬OW (oNO˪nB~)A䤤 $db oӪ󰲳]wgb main.php إߦnFAӥBXӪƴNOWҤlC

      test3.php:
      <?php
          require "main.php";
          // إ߲Ĥ@h}C
          $category = array();
          $db->setSQL($SQL1, 'CATEGORY');
          if (!$db->query('CATEGORY')) die($db->error());
          // Ĥ@hj骺
          while ($item_category = $db->fetchAssoc('CATEGORY'))
          {
              // إ߲ĤGh}C
              $topic = array();
              $db->setSQL(sprintf($SQL2, $item_category['category_id']), 'TOPIC');
              if (!$db->query('TOPIC')) die($db->error());
              
      // ĤGhj骺
              while ($item_topic = $db->fetchAssoc('TOPIC'))
              {
                  
      // ƱJĤGh}C
                  array_push($topic, $item_topic);
              }

              
      // ĤGh}CwĤ@h}CҧƤ@Ӧ
              $item_category['topic'] = $topic;
              // Ĥ@hƱJĤ@h}C
              array_push($category, $item_category);
          }
          $tpl->assign("forum", $category);
          $tpl->display("test3.htm");
      ?>

      bƮw@ƫAڭ̱o쪺O@ӥ]tӵƪ}CCzL while ԭz array_push 禡Aڭ̱NƮwƤ@@}C̡CpGzuΨhjANĤGhj (⪺) hYiC


    7. MweO_
    8. nMwO_ܤeAڭ̥iHϥ if oӻykӰܡCҦppGϥΪ̤wgnJܡAڭ̪˪NiHo˼gG

      <{if $is_login == true}>
      ܨϥΪ̾ާ@
      <{else}>
      ܿJbMKX
      <{/if}>

      n`NOAu==v@wnUdܤ֤@ӪťզrA_h Smarty |LkѪRC

      if yk@몺ΥiHѷөxϥλAҥH̦bo̴Nԥ[ФFCL̵o{F@Ӧ쪺ΡG``|ݨ{̭nͳo˪@ӪG (ƦrNOƶ)

      1 2
      3 4
      5 6
      7 8

      oӵ̺٤uVЪvCSMDzΪaVФPAeX`ڭ̬ݨ쪺Ъ泣OqWӤUA@Cu@ơCӾVЪhiHVab@C n ƫAAU@CAӰj鵲CnFo˪\A²檺覡uݭn section M if ftYiC

      ڭ̨ӬݬݤUoӨҤlG

      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');
      ?>

      ˪gkpUG

      templates/test4.htm:
      <html>
      <head>
      <title>VЪ</title>
      </head>
      <body>
      <table width="500" border="1" cellspacing="0" cellpadding="3">
          <tr>
          <{section name=sec1 loop=$my_array}>
              <td><{$my_array[sec1].value}></td>
          <{if $smarty.section.sec1.rownum is div by 2}>
          </tr>
          <tr>
          <{/if}>

          <{/section}>
          </tr>
      </table>
      </body>
      </html>

      Ib $smarty.section.sec1.rownum o Smarty ܼơAb section j餤oܼƷ|oq 1 }l ޭȡAҥH rownum Q 2 ɮɡANX </tr><tr> Ϫ洫C (`NIO </tr> be <tr> b᭱) C]Ʀr 2 NOڭ̦b@CQne{ƵơCUiHѦhܤƨLPe{覡C


    9. J~e
    10. ڭ̥iHb˪J PHP {XάOt@Ӥl˪AOOϥ include_php include o Smarty ˪ykF include_php ̸֥ΡAϥΤ覡iHdߩxUAo̤AԭzC

      bϥ include ɡAڭ̥iHwJl˪AάOʺAJl˪CwJq`ϥΦb@PYΪvŧiFӰʺAJhiHΦbΤ@ج[AӶi@BFp Winamp i Skin CMoاڭ̤]iHVΡApөwC

      ڭ̨ӬݬݤUoӨҤlG

      test5.php:
      <?php
          require "main.php";
          $tpl->assign("title", "Include ");
          $tpl->assign("content", "oO˪ 2 ܼ");
          $tpl->assign("dyn_page", "test5_3.htm");
          $tpl->display('test5_1.htm');
      ?>

      ˪ 1 gkpUG

      templates/test5_1.htm:
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=big5">
      <title><{$title}></title>
      </head>
      <body>
      <{include file="test5_2.htm"}><br />
      <{include file=$dyn_page}>
      <{include file="test5_4.htm" custom_var="ۭqܼƪe"}>
      </body>
      </html>

      ˪ 2 gkpUG

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

      ˪ 3 gkpUG

      templates/test5_3.htm:
      oO˪ 3 e

      ˪ 4 gkpUG

      templates/test5_4.htm:
      <{$custom_var}>

      o̪`NXӭIG1. ˪mOHewq template_dir ǡF2. Ҧ include iӪl˪AܼƤ]|QĶCF3. include iHΡuܼƦW=ܼƤevӫwާtiӪ˪ҥ]tܼơApPW˪ 4 kC