:::
4-4 製作共同頁首檔 header.php
- 這裡的共同頁首檔指的是程式「一開始」每個檔案都會執行的程式,可以集中在同一個檔
- 建立
header.php<?php require 'function.php'; require 'vendor/autoload.php'; use Smarty\Smarty; $smarty = new Smarty(); - 修改
index.php並載入header.php<?php require 'header.php'; // 過濾外部傳來變數 $op = filter_input_var('op'); switch ($op) { case 'embed': # code... break; default: $op = "main"; break; } $smarty->assign('now_op', $op); $smarty->display('index.tpl'); - 修改
admin.php並載入header.php<?php require 'header.php'; // 過濾外部傳來變數 $op = filter_input_var('op'); switch ($op) { default: $op = "publish"; break; } $smarty->assign('now_op', $op); $smarty->display('admin.tpl'); - 如此一來,
index.php及admin.php的結構就非常像,更清楚易懂,也更好維護了。
4-3 修改編輯表單使之可以送出資料