Smarty安裝

安裝Smarty發行版在/libs/目錄裏的庫檔(就是解壓了). 這些php檔你可不能亂畫哦.這些檔被所有應用程式共用,也只能在你升級到新版的smarty的時候得到更新。

Smarty手冊範例 2-1.Smarty庫檔

Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
/core/*.php (all of them)
/plugins/*.php (all of them)

Smarty使用一個叫做'SMARTY_DIR'的php常量作為它的系統庫目錄。基本上,如果你的應用程式可以找到 Smarty.class.php檔,你不需要設置SMARTY_DIR,Smarty將會自己運作。但是,如果 Smarty.class.php沒有在你的include_path(php.ini裏的一項設置)裏,或者沒有在你的應用程式裏設置它的絕對路徑的時候,你就必須手動配置SMARTY_DIR 了(大多數程式都如此)SMARTY_DIR必須包含結尾斜杠。

這裏是你在你的php腳本裏創建一個smarty的應用實例的例子:

require('Smarty.class.php');
$smarty = new Smarty;

試著運行一下以上腳本,如果你發現"未找到Smarty.class.php 檔"的錯誤時,你應該這樣做:

Smarty手冊範例 2-3.加入庫檔目錄的絕對路徑

require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty;

Smarty手冊範例 2-4.在include_path加入庫檔目錄

// Edit your php.ini file, add the Smarty library
// directory to the include_path and restart web server.
// Then the following should work:
require('Smarty.class.php');
$smarty = new Smarty;

Smarty手冊範例 2-5.手工設置SMARTY_DIR常量

define('SMARTY_DIR','/usr/local/lib/php/Smarty/');
require(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty;


現在庫檔已經搞定,該是設置為你的應用程式配置其他有關Smarty的目錄的時候了。Smarty要求4個目錄,默認下命名為:tempalatestemplates_cconfigs and cache。每個都是可以自定義的,可以修改Smarty類屬性: $template_dir$compile_dir$config_dir, and $cache_dir respectively。強烈推薦你為每個用到smarty的應用程式設置單一的目錄!

確定你已經知道了你的web伺服器檔根目錄。在我們的例子裏,檔根目錄是:"/web/www.mydomain.com/docs/"Smarty的4個目錄 只可以被那些庫檔訪問,不可以被網路上的流覽器訪問的目錄。因此為避免任何安全問題,要求將那4個目錄和網頁檔目錄(就是流覽器看的)分開來。


在我們的安裝例子裏,我們將為一個留言板程式配置smarty環境。我們挑選應用程式只為了實現目錄命名約定。你可以對任何程式使用相同的環境,只要將"guestbook"改成你要的名字就可以了。我們將把Smarty目錄放在 "/web/www.mydomain.com/smarty/guestbook/"下。

在你的文檔目錄下至少得有一個檔,這個檔可以被流覽器訪問.我們叫它 "index.php"好了.把它放到"/guestbook/"目錄下.

技術提示:建立web伺服器很方便,這個檔可以被web伺服器自動識別。如果你訪問"http://www.mydomain.com/guestbook/",你不需要在URL上輸入"index.php",index.php腳本就可以被執行。在Apache伺服器中,可以通過在DirectoryIndex的後面添加"index.php" 檔(用反斜杠分開每個入口)來完成設置。


現在我們看看這些檔結構:

Smarty手冊範例 2-6.例子的檔結構

/usr/local/lib/php/Smarty/Smarty.class.php
/usr/local/lib/php/Smarty/Smarty_Compiler.class.php
/usr/local/lib/php/Smarty/Config_File.class.php
/usr/local/lib/php/Smarty/debug.tpl
/usr/local/lib/php/Smarty/core/*.php
/usr/local/lib/php/Smarty/plugins/*.php

/web/www.mydomain.com/smarty/guestbook/templates/
/web/www.mydomain.com/smarty/guestbook/templates_c/
/web/www.mydomain.com/smarty/guestbook/configs/
/web/www.mydomain.com/smarty/guestbook/cache/

/web/www.mydomain.com/docs/guestbook/index.php

Smarty的 $compile_dir 和$cache_dir必須可寫。通常是user "nobody" 和 group "nobody"。如果是 OSX用戶,默認為user "web" 和 group "web"。如果你在使用Apache,你可以看看httpd.conf 檔 (通常在"/usr/local/apache/conf/"目錄下)哪些user和group正在被使用。

Smarty手冊範例 2-7 檔許可權設置

chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/templates_c/
chmod 770 /web/www.mydomain.com/smarty/guestbook/templates_c/

chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/cache/
chmod 770 /web/www.mydomain.com/smarty/guestbook/cache/

技術提示: 
chmod 770相當安全了,它只讓user "nobody" 和 group "nobody" 讀/寫 訪問。如果你要對任何人開放讀取訪問許可權(大多是為了你自己查看檔),你可以使用 775。

我們需要創建index.tpl檔讓smarty載入.這個檔放在 $template_dir目錄裏。

Smarty手冊範例 2-8 編輯/web/www.mydomain.com/smarty/templates/index.tpl

{* Smarty *}

Hello, {$name}!

技術提示:
{* Smarty *} 是一個範本注釋。雖然並不是必須的,但是這可以很好的鍛煉你在範本檔裏加入注釋的習慣。它可以使檔便於識別。例如,一些文本編輯器可以識別這個檔,並加以語法高亮顯示。

現在來編輯index.php。我們將創建一個Smarty的實例,指派範本變數,顯示 index.tpl檔。在我們的例子的環境裏, "/usr/local/lib/php/Smarty"已經包括在了 include_path裏了。

Smarty手冊範例 2-9.編輯/web/www.mydomain.com/docs/guestbook/index.php

// load Smarty library
require('Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
$smarty->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
$smarty->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$smarty->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';

$smarty->assign('name','Ned');

$smarty->display('index.tpl');

技術提示:
在我們的例子裏,已經設置了所有Smarty目錄的絕對目錄。如果 '/web/www.mydomain.com/smarty/guestbook/' 已經包括在 include_path裏了,那麼這些設置則沒有必要。但是,從經驗和通用性看來,為避免發生錯誤,還是配置一下為好。

現在在流覽器打開 index.php,你應該看到"Hello, Porky!"

你現在已經完成了Smarty的基本設置,恭喜!!



上一篇: 下一篇: Smarty擴展設置