:::

3-2 建立前、後台樣板檔並套用之

 

  1. 我們可以先將原本的 index.html,另存為 templates/index.tpl(或將 index.html 拖到 templates 下,按 F2 修改副檔名),作為前台樣板檔
  2. 同時將原本的 admin.html,另存為 templates/admin.tpl(或將 admin.html 拖到 templates 下,按 F2 修改副檔名),作為後台樣板檔
  3. 利用樣板引擎的 display() 方法就可以套用樣板檔(無須指定 templates 目錄)。
  4. 修改 index.php  在最後一行加入欲套用 index.tpl 樣板的語法:
    <?php
    require 'vendor/autoload.php';
    use Smarty\Smarty;
    $smarty = new Smarty();
    $smarty->display('index.tpl');

     

  5. 修改 admin.php 在最後一行加入欲套用 admin.tpl 樣板的語法:

    <?php
    require 'vendor/autoload.php';
    use Smarty\Smarty;
    $smarty = new Smarty();
    $smarty->display('admin.tpl');

     

  6. 如此,執行 http://localhost/php8  便可看到套用後結果囉!
  7. 順利的話,應該和直接觀看 index.htmladmin.html 一模一樣才是。
  8. 不過,目前PHP和樣板並沒有真的結合起來!!我們只是確認了Smarty有在運行而已,顯示的內容依舊是之前寫死的。

 

 


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1998%26tbsn%3D55

計數器

今天: 2813281328132813
昨天: 2259225922592259
總計: 7951611795161179516117951611795161179516117951611