2011-03-16 17:43:27 robin

安裝Smarty的方法

安裝Smarty的方法

Source: http://smarty.php.net/quick_start.php


先去http://smarty.php.net 抓smarty下來

$> cd 抓下來的目錄
$> gtar -zxvf Smarty-2.6.7.tar.gz
$> mkdir /usr/local/lib/php/Smarty
$> cp -r Smarty-2.6.7/libs/* /usr/local/lib/php/Smarty

現在因該會有像下面這樣的目錄來有檔案:

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


設定 Smarty 的目錄

要裝好Smarty, 你會需要4個目錄. 4個目錄分別用來裝
templates, compiled templates,
cached templates 還有 config files.

你可能不需要用到caching 或是 config files,
但是裝來比較好啦, 也許以後會需要.

建議把這4個目錄放在web server目錄的外面,
如果你的 web server 目錄是 /var/www/,
那建議你放在/var/裡面.

注意噢, web server PHP 的使用者需要可以
寫入 cache 和 compile directories 的權限.

範例:
假設放網站檔案的目錄是/var/www/
web server 的 username 是 "www-data"
(www-data是ubuntu裡面預設的).

我們會把Smarty的檔案放在 /var/smarty/ 裡

$> cd /var
$> mkdir smarty
$> mkdir smarty/templates
$> mkdir smarty/templates_c
$> mkdir smarty/cache
$> mkdir smarty/configs
$> chown www-data:www-data smarty/templates_c
$> chown www-data:www-data smarty/cache
$> chmod 775 smarty/templates_c
$> chmod 775 smarty/cache

基本上, 這樣 Smarty 就安裝完成了


設定 SMARTY PHP SCRIPTS

現在我們就來設定
Now we setup our application in the document root:

$> cd /var/www
$> mkdir myapp
$> cd myapp
$> vim index.php

編輯 index.php, 然後把下面這段html打進去:
// put full path to Smarty.class.php
require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = '/var/smarty/templates';
$smarty->compile_dir = '/var/smarty/templates_c';
$smarty->cache_dir = '/var/smarty/cache';
$smarty->config_dir = '/var/smarty/configs';

$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');
?>


設定 SMARTY TEMPLATE

$> vim /var/smarty/templates/index.tpl

編輯 index.tpl, 然後把下面這段html打進去:


Smarty


Hello, {$name}!