帳號登錄
帳號:
密碼:
記住我 | 忘記密碼 | 帳號註冊
網站導覽
模組開發 : 在模組中使用 Smarty 樣版-編輯一個 Smarty 樣版
作者 elie 於 2010年09月03日 20:46:27 (6727 次閱讀)

原始出處 http://www.xoops.org/modules/mediawiki/index.php?title=Using_Smarty_templates_in_a_module

 

編輯一個Smarty 樣版

Smarty 樣版是html 頁面參雜特殊符號的程式碼,在XOOPS 中使用的是<{}>。舉例來說,要在smarty 樣版中取得XOOPS 網址的話就要使用:<{$xoops_url}>。

為了讓本文件保持簡潔,我採用讓程式碼不會太複雜的方式撰寫。將index.php 改名為index_original.php,接著建立一個新的index.php 檔案內容如下:

<?php

// Tutorial

// Created by KaotiK

require('../../mainfile.php');

$xoopsOption['template_main'] = 'tut_main.html';

require(XOOPS_ROOT_PATH.'/header.php');

 

$xoopsTpl->assign('mytest', 'Hello World!');

 

require(XOOPS_ROOT_PATH.'/footer.php');

?>

在這裡有兩行新的程式碼。第一個$xoopsOption 是告訴XOOPS 在這個檔案中使用哪個樣版,在這個範例中使用的是tut_main.html。第二行是使用smarty 產生動態網頁的方式;我們可以用這個方式將php 變數指定給smarty 變數。這是什麼意思呢?範例中smarty 樣版的變數'mytest' 將會被'Hello World!' 取代。現在打開樣版檔/templates/tut_main.html 並寫入:<{$mytest}>。注意:如果你是使用如dreamweaver 的所見即所得編輯器的話要在程式碼模式中編輯檔案。
點選主選單的tutorial 你會看到:Hello World!
相當好!現在我們的模組已經有了一個smarty 樣版了。現在讓我們實作part 1 的功能吧。

在part 1 有一個四個欄位的表格顯示資料庫內的資料,現在開始在smarty 樣版中建立表格抬頭。清除樣版中的<{$mytest}> 後寫上:

<table width="100%" border="0" cellspacing="0" cellpadding="0">

 <tr>

 <td><{$smarty.const.TT_NAME}></td>

 <td><{$smarty.const.TT_ADDRESS}></td>

 <td><{$smarty.const.TT_TELEPHONE}></td>

 <td><{$smarty.const.TT_EMAIL}></td>

 </tr>

 </table>

不要忘了使用編輯器的程式碼模式編輯。

再清除樣版快取檔案後點選主選單的tutorial 你就會看到一個四欄的表單。你可以注意到我使用$smarty.const 取得定義在/tutorial/language/english/main.php 的語言常數。另外一個設定常數的方式是使用$xoopsTpl->assign,然而我會建議不要使用這種方式,因為這樣的話每個變數都會多一行程式碼。


可列印模式 轉寄給朋友

The XOOPS organisation ("XOOPS") is committed to protecting the privacy of users of the XOOPS.org sites.
The website used Ver.2.4.5 POWERED BY XOOPS PROJECT.