Re: [請益] 關於Smarty的樣板用法

看板PHP作者 (寶貝豬)時間16年前 (2009/03/01 10:25), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串5/6 (看更多)
方法一跟方法二應該都可以用, 甚至可以混用. 至於要採用哪種方式則 應該從系統架構規劃的角度來看. 底下提供一個設計樣式僅供參考: 資料層 控制層 表達層 +---[response]----+ | v assign DB_Obj<---[SQL]-----list.php--[display/smarty]-->main.tpl | +---[response]----+ +head.tpl | v +footer.tpl DB_Obj1<---[SQL]----tool_1.php---[response]----->+tool_1.tpl ^ | +------[request/ajax]--------+ ※ 引述《sqaure (sqaure)》之銘言: : 我想做的是 : : [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: 211.74.84.3

03/01 12:50, , 1F
精闢
03/01 12:50, 1F
文章代碼(AID): #19gV8hVX (PHP)
文章代碼(AID): #19gV8hVX (PHP)