<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      說明smarty模板

      [摘要]PHP代碼:--------------------------------------------------------------------------------以下的介紹是基于2.5版的,也不知有BUG沒,OS是win2000,php應該4.1以后的就可以。,兄弟只是開發時使用過幾次,...
      PHP代碼:--------------------------------------------------------------------------------



      以下的介紹是基于2.5版的,也不知有BUG沒,OS是win2000,php應該

      4.1以后的就可以。,兄弟只是開發時使用過幾次,沒太深的研究,

      介紹的在手冊上都能找到,如果有錯誤之處難免多包涵吧


      smarty模板據說是php.net推薦使用的,據說功能比較強,據說速度

      也不慢,據說用的人越來越多<img src=http://cfan.net.cn/info/"images/smilies/biggrin.gif" border="0" alt="">

      官方網站smarty.php.net上面有論壇,有下載,有手冊。有你想要的一切,切入正題:
      (-)安裝:
      下載的包解壓后有三個下級目錄:模板文件目錄是<libs> 其下有四個類文件一個目錄.首先介紹最重要的:Smarty.class.php它應該是整個smarty模板的核心類,平時應用時也是跟它打交道,在你的應用程序目錄下建立以下目錄結構:
      /app_1/smarty/libs
      /app_1/smarty/templates_c
      /app_1/smarty/templates
      /app_1/smarty/configs

      <libs>對應壓縮包下的libs目錄下面放smarty需要的類文件
      <templates>存放模板文件,程序用到的模板文件都放這里
      <configs>存放模屬性文件,以后詳細說
      <templates_c>存放"編譯"后的模板文件,以后詳細說,些目錄需要設為777
      <cache>存放緩存的模板文件

      以上文件名為smarty默認的文件名,用戶可以指定不同的文件名,如:guest_template_dir,admin_template_dir 等.如果不指定將使用以上文件名
      (二)使用:

      設計模板:
      1模板變量:{$變量名},如 {$color},{$type}
      test_color.tpl
      <font color={$color}>{$name}</font>
      test_url.tpl
      <a href="{$url}" target="{$target}">{$title}</a>

      2也可以使用模板數組
      test_array.tpl
      {people.name}<br>
      {people.sex}<br>
      {people.money}<br>
      3模板使用區塊列表
      如:
      user1 user1_sex user1_money
      user2 user2_sex user2_money
      user3 user3_sex user3_money

      section.tpl
      可以用表格格式化:
      <TABLE>
      {section name=user loop=$userList}
      <TR>
      <TD>{$userList[user].name}</TD>
      <TD>{$userList[user].sex}</TD>
      <TD>{$userList[user].money}</TD>
      </TR>
      {/section}
      </TABLE>


      <?php
      //當前目錄app_1下
      //生成$smarty實例

      require('smarty/lib/Smarty.class.php');
      $smarty = new Smarty;

      //指定功能目錄,可以自定義
      $smarty->template_dir = 'smarty/templates';
      $smarty->$compile_dir = 'smarty/template_c';

      //為模板變量賦值 模板:color.tpl 放于smarty/templates下
      //$smarty->assign('模板變量名','php內部變量');
      //$smarty->display(模板文件名);

      $smarty->assign('color','red');
      $smarty->assign('name','hello world');

      //顯示模板
      //輸出:<font color=red>helloworld</font>
      $smarty->display('test_color.tpl');

      //為模板數組變量賦值,模板:test_array.tpl
      //輸出:

      //巴豆
      //男
      //a litte
      $people = array('name'=>'巴豆','sex'=>'男','money'=>'a little');
      $smarty->assign('people',$people);
      $smarty->display('test_color.tpl');

      //模板區塊
      //{section name=user loop=$userList}
      //section:標簽功能
      //name:標簽名
      //loop:循環數組
      //功能循環輸出多行
      //輸出:

      //user1 user1_sex user1_money
      //user2 user2_sex user2_money
      //user3 user3_sex user3_money


      $userList[] = array('name'=>'user1','sex'=>'user1_sex','money'=>'user1_money');
      $userList[] = array('name'=>'user2','sex'=>'user2_sex','money'=>'user2_money');
      $userList[] = array('name'=>'user3','sex'=>'user3_sex','money'=>'user3_money');

      $smarty->assign('userList',$userList);
      $smarty->display('test_section.tpl');
      ?>
      暫時先寫這么點都是最基本的東西了,代碼沒有測試,兄弟初次寫東西有出錯的地方一概不負責。
      還有好多有意思的功能以后有機會再寫<img src=http://cfan.net.cn/info/"images/smilies/biggrin.gif" border="0" alt="">



      日韩精品一区二区三区高清