:::
主內容區域
12-4 關於 $xoopsTpl 樣板物件
- XOOPS 2.5.10 (含)以下的樣板引擎是使用 smarty 2.6.x
- XOOPS 2.5.11 則是使用新的 smarty 3.x
- 頁面的主樣板檔用
$xoopsOption['template_main']來指定,需註冊到xoops_version.php中的$modversion['templates']中。 - 樣板檔副檔名可為 .html 或 .tpl(建議)
- XOOPS的樣板引擎物件為
$xoopsTpl,在php檔中套用變數到樣板的語法為:$xoopsTpl->assign('樣板變數名稱' , '變數值'); - 在樣板中,則是使用以下方式來呈現變數:
<{$樣板變數名稱}> - 如果是陣列,則用
.來顯示,例如:<{$陣列.索引}> - Smarty保留字(https://www.smarty.net/docs/en/language.variables.smarty.tpl),常用的以下這些:
- 可以顯示常數
<{$smarty.const.常數}> - 可以顯示各種超級全域變數
<{$smarty.get.變數}> <{$smarty.post.變數}> <{$smarty.session.變數}> <{$smarty.cookie.變數}> <{$smarty.server.變數}> - 還可以抓取目前時間(這是時間戳記)
<{$smarty.now}>
- 可以顯示常數
- XOOPS中的Smarty註解
<{* 這裡面的東西不會被編譯 *}> - 可以套用PHP的函數(strlen是算文字長度)
<{$title|strlen}>也可以加參數(標題只截出前面數來30個字)
<{$title|mb_substr:0:30}> - 可以在樣板中設定新變數
<{assign var=名稱 value=值}> - 可把流程中的每個需顯示的op動作,獨立做成樣板檔,然後自動引入(
$xoops_rootpath就是實際張裝路徑,不用改)<{include file="$xoops_rootpath/modules/模組名稱/templates/op_`$op`.tpl"}> - Smarty處理各種陣列的範例
傳送內容 PHP檔(*.php) Smarty樣板檔(*.tpl) 一般變數 $action = "活動名稱"; $xoopsTpl->assign('action', $action);<{$action}>一維陣列 $action['title'] = "活動名稱"; $action['content'] = "活動內容"; $smarty->assign('action', $action);<h1><{$action.title}></h1> <{$action.content}>二維陣列 $actions[0]['title'] = "活動1名稱"; $actions[0]['content'] = "活動1內容"; $actions[1]['title'] = "活動2名稱"; $actions[1]['content'] = "活動2內容"; $smarty->assign('actions', $actions);<{foreach from=$actions item=action}> <h1><{$action.title}></h1> <{$action.content}> <{/foreach}> 或 <h1><{$actions.0.title}></h1> <{$actions.0.content}> <h1><{$actions.1.title}></h1> <{$actions.1.content}> - Smarty迴圈用來處理陣列,常用方法如下(二維索引):
<{foreach from=$來源變數 item=樣板變數 name=迴圈別名}> <{$樣板變數.索引}> <{foreachelse}> 該變數沒有值時要出現的內容 <{/foreach}> - 迴圈還有一些特別的用法:
<{$smarty.foreach.迴圈別名.first}> 迴圈第一圈<{$smarty.foreach.迴圈別名.last}>迴圈最後一圈<{$smarty.foreach.迴圈別名.index}>取得迴圈的索引值,依序輸出0、1、2......<{$smarty.foreach.迴圈別名.iteration}>取得迴圈的計數值,依序輸出1、2、3......<{$smarty.foreach.迴圈別名.total}>取得迴圈執行總數
- 詳情可見:https://www.smarty.net/docsv2/en/language.function.foreach.tpl
- 也可以用判斷式
<{if $smarty.now > $end_time}> 活動已結束 <{elseif $smarty.now > $start_time}> 活動開始 <{else}> 活動尚未開始 <{/if}>
12-3 關於 $xoopsDB 資料庫物件