:::
主內容區域
3-2 建立前、後台樣板檔並套用之
- 我們可以先將原本的
index.html,另存為templates/index.tpl(或將index.html拖到templates下,按 F2 修改副檔名),作為前台樣板檔 - 同時將原本的
admin.html,另存為templates/admin.tpl(或將admin.html拖到templates下,按 F2 修改副檔名),作為後台樣板檔 - 利用樣板引擎的
display()方法就可以套用樣板檔(無須指定templates目錄)。 - 修改
index.php在最後一行加入欲套用index.tpl樣板的語法:<?php require 'vendor/autoload.php'; use Smarty\Smarty; $smarty = new Smarty(); $smarty->display('index.tpl'); -
修改
admin.php在最後一行加入欲套用admin.tpl樣板的語法:<?php require 'vendor/autoload.php'; use Smarty\Smarty; $smarty = new Smarty(); $smarty->display('admin.tpl'); - 如此,執行 http://localhost/php8 便可看到套用後結果囉!
- 順利的話,應該和直接觀看
index.html或admin.html一模一樣才是。

- 不過,目前PHP和樣板並沒有真的結合起來!!我們只是確認了Smarty有在運行而已,顯示的內容依舊是之前寫死的。
3-1 安裝Smarty樣板引擎