鍍金池/ 教程/ PHP/ Smarty安裝
Smarty安裝
Smarty insert
Smarty 建立緩存
Smarty調(diào)試控制臺
Smarty if,elseif,else
Smarty include_php
Smarty多個緩存
Smarty方法
Smarty section,sectionelse
Smarty注釋代碼
Smarty屬性
Smarty緩沖處理函數(shù)
Smarty變量調(diào)節(jié)器
Smarty函數(shù)
Smarty組合修改器
Smarty雙引號里值的嵌入
Smarty預過濾器
Smarty foreach,foreachelse
Smarty include
Smarty Caching緩存
Smarty變量
Smarty assign用法
Smarty控制插件輸出緩沖
Smarty從配置文件讀取的變量
Smarty對象
Smarty literal
Smarty緩存集合
Smarty教程
Smarty display方法
Smarty自定義函數(shù)
Smarty配置文件
Smarty擴展設置
Smarty數(shù)學運算
Smarty輸出濾鏡
Smarty fetch方法

Smarty安裝

安裝Smarty發(fā)行版在/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常量作為它的系統(tǒng)庫目錄?;旧?如果你的應用程序可以找到 Smarty.class.php文件,你不需要設置SMARTY_DIR,Smarty將會自己運作。但是,如果 Smarty.class.php沒有在你的include_path(php.ini里的一項設置)里,或者沒有在你的應用程序里設置它的絕對路徑的時候,你就必須手動配置SMARTY_DIR 了(大多數(shù)程序都如此)SMARTY_DIR必須包含結尾斜杠。

這里是你在你的php腳本里創(chuàng)建一個smarty的應用實例的例子:

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

試著運行一下以上腳本,如果你發(fā)現(xiàn)"未找到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;


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

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


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

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

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


現(xiàn)在我們看看這些文件結構:

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。

我們需要創(chuàng)建index.tpl文件讓smarty載入.這個文件放在 $template_dir目錄里。

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

{* Smarty *}

Hello, {$name}!

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

現(xiàn)在來編輯index.php。我們將創(chuàng)建一個Smarty的實例,指派模板變量,顯示 index.tpl文件。在我們的例子的環(huán)境里, "/usr/local/lib/php/Smarty"已經(jīng)包括在了 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');

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

現(xiàn)在在瀏覽器打開 index.php,你應該看到"Hello, Porky!"

你現(xiàn)在已經(jīng)完成了Smarty的基本設置,恭喜!!