[請益] 關於Smarty的樣板用法
我想做的是 :
[list.php]
$smarty = new $smarty();
//....相關設定後
$smarty->assign("content" , "list.tpl");
//query articles....
$smarty->assign("article" , articles);
$smarty->display("main.tpl");
[main.tpl]
{include file="head.tpl"}
-------┬-------------------------
tool |
div1 | {include file = $content}
------┤
tool |
div2 |
: |
-------┴--------------------------
{include file="footer.tpl"}
main.tpl 是一個共用的樣板
主要架構就是 頁面的header跟footer都是固定的靜態樣板(方便未來可替換用)
直接利用smarty { include file="header.tpl"} { include file="footer.tpl"}
就可以解決,
而畫面中主要的內容區 主要是利用assign $content 來選擇想顯示的樣板(例如
$content= list.tpl (文章列表) )
同時把$content的樣板中的資料 assign 進去
最後在整個 $smarty->display("main.tpl") 就會顯示出所有畫面
這是的初步構想 也的確是work的
但比較大的問題就是 左方的功能列 是類似一個個的小視窗
每個小視窗都希望 未來是可以獨立運作的
例如 : tool_1.php <---> tool_1.tpl
請問這樣該怎麼做呢?
做法一 : 如同$content
{include file="tool_1.tpl"}
同時在list.php 當中 assign tool_1.tpl中所需要的資料
缺點 : 如果tool_1.tpl 又需要 給其他頁面共用
assign 給 tool_1.tpl的資料 又要再重複寫一次
作法二 : 利用ajax 的方式 送出request 要求tool_1.php
將responseText貼回 小視窗的div中
缺點 : 寫法較為困難,後續維護需要更高技巧
其tool_1的相關js 與css,又要另外請求利用javascript 加入link到
list.tpl的DOM中
目前我是採取 做法二 ,但不知道是否各位有更好的做法?
因為目前所開發的網站中 會有很多如此共用的頁面
所以才想要避免程式碼重複寫,以便維護
希望能聽聽大家的意見。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.162.209
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 6 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章