SmartTemplate學習筆記

          目錄
          1、SmartTemplate的效率
          2、基本變量
          3、邏輯運算結構
          4、模式(Methods)
          5、擴展類(Extensions) 未完成
          ------------------------------------------------------------------------
          1、SmartTemplate的效率
          雖然他有很多的程序來形成強大的功能,但在執行時只有你調用的才被導入,所以不用擔心這方面的速度影響,同樣這套模版系統是為最快的執行效率而優化過的,比起目前市場上常見的Smarty,要快不少(Smarty采用后期緩存,所以比較可能不是很準確)。

           

          2、SmartTemplate的變量
          Array的變量是由SmartTemplate內建函數assign()來賦值的
          具體語法如下

          code:
          assign ( 模版中的變量, 要替換的內容 )

          code:
          assign ( Array內容 )

          正如其他程序的變量一樣,smartTemplate的變量是由特殊的{}所包含的。里面的內容可以是String,Array,Int,或者是Long Text等等(基本上php支持的)
          在儲存Array數據時,smartTemplate運用了我們常見的父子級分割符".",所以一個特殊的Array數據由Array Handle和具體位置的索引組成(Numerical Index or Associative Index)。
          下面是一個例子
          在php環境下運行以下程序

          code:
          <?php
             $template  =  new SmartTemplate('template.html');
             $text  =  'Sample Text';
             $template->assign( 'TITLE', $text );
             $template->output();
          ?>

          模版

          code:
          <html> {TITLE} </html>

          輸出

          code:
          <html> Sample Text </html>

          在只有一個Array的情況下,可以直接省略前面的array handle,就象在使用javascript時,document.window.close()可以省略為window.close()

          code:
          <?php
             $user  =  array(
                          'NAME'  => 'John Doe',
                          'GROUP' => 'Admin',
                          'AGE'   => '42',
                       );
             $template  =  new SmartTemplate('user.html');
             $template->assign( $user );
             $template->output();
          ?>

          模版

          code:
          Name:  {NAME}
          Group: {GROUP}
          Age:   {AGE}
          輸出
          CODE 
          Name:  John Doe
          Group: Admin
          Age:   42


          下面是另外一個例子。使用了SmartTemplate的循環函數<!-- begin Array名 -->XXXXXX<!-- end Array名>
          他的功能類似foreach(),只要有東西,就一直循環顯示

          code:
          <?php
             $links  =  array(
                            array(
                                'TITLE' => 'PHP',
                                'URL'   => 'http://www.php.net/',
                            ),
                            array(
                                'TITLE' => 'Apache',
                                'URL'   => 'http://www.php.net/',
                            ),
                            array(
                                'TITLE' => 'MySQL',
                                'URL'   => 'http://www.mysql.com/',
                            ),
                        );
             $template  =  new SmartTemplate('links.html');
             $template->assign( 'links', $links );
             $template->output();
          ?>

          HTML模版

          code:
          <html>
          <h3> Sample Links </h3>
          <!-- BEGIN links -->
             <a href="../{URL}"> {TITLE} </a>
          <!-- END links -->
          </html>
          CODE 
          <html>
          <h3> Sample Links </h3>
             <a href="../   <a href="../   <a href="../</html>

          3、SmartTemplate的邏輯控制結構
          ★If和end If
          語法:
          <!-- IF 變量 --> 變量已被賦值! <!-- ENDIF 變量 -->
          如果IF后面直接跟變量,變量為Null時會返回0,否則返回1
          <!-- IF name=="John Doe" --> Your name is John Doe! <!-- ENDIF name -->
          ==判斷是否相等,如果相等返回1,不相等返回0
          <!-- IF name!="John Doe" --> Your name is not John Doe! <!-- ENDIF name -->
          !=判斷是否不等,如果成立返回1,相等則返回0
          例子:

          code:
          <?php
             require_once "class.smarttemplate.php";
             $page = new SmartTemplate("if.html");
             $page->assign( 'username',   'John Doe' );
             $page->assign( 'usergroup',  'ADMIN' );
             $page->assign( 'picture',    '' );
             $page->output();
          ?> 

          HTML

          code:
          <!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF -->
          <!-- IF picture --> <img src="{picture}"> <!-- ENDIF picture -->
          <!-- IF usergroup="ADMIN" -->
          <a href="../admin.php"> ADMIN Login </a><br>
          <!-- ENDIF usergroup -->

          輸出

          code:
          <H3> Welcome, John Doe </H3>
          <a href="../admin.php"> ADMIN Login </a><br>

          ★IF的子局 else
          如果else子句出現在一個邏輯循環中,當if的條件不成立時則會被運行。
          例子

          code:
          <?php
             require_once "class.smarttemplate.php";
             $page = new SmartTemplate("else.html");
             $page->assign( 'username',   'John Doe' );
             $page->assign( 'usergroup',  'ADMIN' );
             $page->assign( 'picture',    '' );
             $page->output();
          ?> 

          模版

          code:
          <!-- IF username -->
          <H3> Welcome, {username} </H3>
          <!-- ENDIF -->
          <!-- IF picture -->
          <img src="{picture}">
          <!-- ELSE -->
          Picture not available! <br>
          <!-- ENDIF picture -->
          <!-- IF usergroup="ADMIN" -->
          <a href="../admin.php"> ADMIN Login </a><br>
          <!-- ELSE -->
          You are in guest mode!
          <!-- ENDIF usergroup -->

          輸出

          code:
          <H3> Welcome, John Doe </H3>
          Picture not available! <br>
          <a href="../admin.php"> ADMIN Login </a><br>

          ★elseif
          elseif是else和if組合起來的一種結構,其意義為"除此之外如果..."
          以下是一個例子

          code:
          <?php
             require_once "class.smarttemplate.php";
             $page = new SmartTemplate("elseif.html");
             $page->assign( 'usergroup',  'INTERNAL' );
             $page->output();
          ?> 

          模版文件

          code:
          <!-- IF usergroup="ADMIN" -->
          <a href="../admin.php"> 管理員登陸 </a><br>
          <!-- ELSEIF usergroup="SUPPORT" -->
          <a href="../support.php"> 幫助人員登陸</a><br>
          <!-- ELSEIF usergroup -->
          <a href="../other.php"> 普通方式登陸 </a><br>
          <!-- ELSE -->
          You don't even have a usergroup!
          <!-- ENDIF -->

          運行php得到的輸出

          code:
          <a href="../other.php"> 普通方式登陸 </a><br>

          ★Begin...End
          這 個語句用于讀取一個整數索引矩陣(Numerical Array,以數字為索引的數組)的值.而每個整數矩陣的子矩陣則成為以字符串為索引的矩陣(Associative Array)然后在<!-- begin --> 和 <!-- end -->之間的語句將會被讀取并且重復執行.
          附加:,每個associative array(字符串為索引的矩陣)會有兩個附加的值
          ROWCNT : 此元素在父矩陣中的具體位置 (0,1,2,3,...n) (就是目前在第幾個矩陣)
          ROWBIT : 矩陣序號的最后一位. (0,1,0,1,0,1,...)
          下面是一個例子
          PHP代碼:

          code:
          <?php
             require_once "class.smarttemplate.php";
             $page = new SmartTemplate("begin_end.html");
             $users = array(
                        array( 'NAME' => 'John Doe',   'GROUP' => 'ADMIN' ),
                        array( 'NAME' => 'Jack Doe',   'GROUP' => 'SUPPORT' ),
                        array( 'NAME' => 'James Doe',  'GROUP' => 'GUEST' ),
                        array( 'NAME' => 'Jane Doe',   'GROUP' => 'GUEST' ),
                      );
             $page->assign( 'users',  $users );
             $page->output();
          ?>

          HTML模版

          code:
          <style type="text/css">
          .col0 { background-color: #D0D0D0; }
          .col1 { background-color: #F0F0F0; }
          </style>
          <table border="1" cellpadding="2" cellspacing="0">
          <tr>
          <th> No </th>
          <th> Username </th>
          <th> Usergroup </th>
          </tr>
          <!-- BEGIN users -->
          <tr class="col{ROWBIT}">
          <td> {ROWCNT} </td>
          <td> {NAME} </td>
          <td> {GROUP} </td>
          </tr>
          <!-- END users -->
          </table>

          最后的輸出

          code:
          <style type="text/css">
          .col0 { background-color: #D0D0D0; }
          .col1 { background-color: #F0F0F0; }
          </style>
          <table border="1" cellpadding="2" cellspacing="0">
          <tr>
          <th> No </th>
          <th> Username </th>
          <th> Usergroup </th>
          </tr>
          <tr class="col0">
          <td> 0 </td>
          <td> John Doe </td>
          <td> ADMIN </td>
          </tr>
          <tr class="col1">
          <td> 1 </td>
          <td> Jack Doe </td>
          <td> SUPPORT </td>
          </tr>
          <tr class="col0">
          <td> 2 </td>
          <td> James Doe </td>
          <td> GUEST </td>
          </tr>
          <tr class="col1">
          <td> 3 </td>
          <td> Jane Doe </td>
          <td> GUEST </td>
          </tr>
          </table>

          ☆smartTemplate的方法
          注:以下列出的方法并不會在模版設計中出前,他們屬于smartTemplate的代碼編輯部分,但是如果為了實現更深一步的模版設計,下面的內容是肯定有用的.
          ★基礎方法:assign (中文意思:賦值)
          語法:
          assign ( 變量名, 混合內容 )
          或者
          assign ( 矩陣變量 )
          更多介紹在變量介紹部分
          ★Append(附加)
          語法:append ( 變量名, 內容 )
          對所提供的變量附加提供的內容
          例子:

          code:
          <?php
             $page  =  new SmartTemplate('links.html');
             $page->append('links' => array(
                                          'TITLE' => 'PHP',
                                          'URL'   => 'http://www.php.net/'
                                      ));
             $page->append('links' => array(
                                          'TITLE' => 'Apache',
                                          'URL'   => 'http://www.apache.org/'
                                      ));
             $page->append('links' => array(
                                          'TITLE' => 'MySQL',
                                          'URL'   => 'http://www.mysql.com/'
                                      ));
             $page->output();
          ?>

          模版

          code:
          <html>
          <h3> Sample Links </h3>
          <!-- BEGIN links -->
             <a href="../{URL}"> {TITLE} </a>
          <!-- END links -->
          </html>

          最終輸出

          code:
          <html>
          <h3> Sample Links </h3>
             <a href="../http://www.php.net/"> PHP </a>
             <a href="../http://www.apache.org/"> Apache </a>
             <a href="../http://www.mysql.com/"> MySQL </a>
          </html>

          另外一個附加字符串的例子:

          code:
          <?php
             $page  =  new SmartTemplate('template.html');
             $page->append('TITLE' => 'Hello ');
             $page->append('TITLE' => 'World ');
             $page->append('TITLE' => '!');
             $page->output();
          ?>

          輸出將會得到

          code:
          <html> Hello World ! </html>
          posted on 2005-08-16 00:26 JRobot 閱讀(180) 評論(0)  編輯  收藏 所屬分類: php相關

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 阿图什市| 特克斯县| 德钦县| 东乌| 垣曲县| 旬邑县| 尚义县| 衡南县| 南华县| 东海县| 垦利县| 来凤县| 铜鼓县| 嵊州市| 会昌县| 大连市| 垣曲县| 叶城县| 吴川市| 松溪县| 西城区| 安西县| 光山县| 宁夏| 马关县| 池州市| 富源县| 金乡县| 阿克| 长岛县| 石渠县| 依安县| 揭西县| 容城县| 长汀县| 东阳市| 宿迁市| 始兴县| 衡南县| 老河口市| 盐津县|