Basic Installation

Install the Smarty library files which are in the /libs/ directory of the distribution. These are the PHP files that you SHOULD NOT edit. They are shared among all applications and they only get updated when you upgrade to a new version of Smarty.

安裝完 /libs/ 目錄下 Smarty 套件的程式庫檔後,在這個目錄下的 PHP 檔案是你 不能夠編輯的,因為這些檔案會被廣泛的應用在所有的應用程式中,只有當你升級 Smarty 後才 可以更新。

Example 2-1. Smarty 程式庫檔

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

Smarty uses a PHP constant named SMARTY_DIR which is the system filepath Smarty library directory. Basically, if your application can find the Smarty.class.php file, you do not need to set SMARTY_DIR, Smarty will figure it out on its own. Therefore, if Smarty.class.php is not in your include_path, or you do not supply an absolute path to it in your application, then you must define SMARTY_DIR manually. SMARTY_DIR must include a trailing slash.

Smarty 使用一個 SMARTY_DIR 的 PHP 常數變數,而這個路徑是 Smarty 程式庫檔的目錄。基本上,如果你的套件可以找到 Smarty.class.php 這個檔案的話你就不需要設定 SMARTY_DIR 這個變數了,因為 Smarty 會根據預設變數設定 環境。因此,如果你的 include_path 沒有 Smarty.class.php,或者是在你的應用程式中並不是使用絕對路徑的話,你就需要手動定義 SMARTY_DIR 了。SMARTY_DIR 必須 包含最後面的斜線。

>

Here is how you create an instance of Smarty in your PHP scripts:

以下是在你的 PHP 程式中產生一個 Smarty 實例的方式:

Example 2-2. 產生一個 Smarty 的實例

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

Try running the above script. If you get an error saying the Smarty.class.php file could not be found, you have to do one of the following:

執行上述程式,如果你在執行之後有錯誤訊息 "Smarty.class.php file could not be found" 的話,你就必須作以下的動作:

Example 2-3. 提供程式庫檔案的絕對路徑

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

Example 2-4. 在 PHP include_path 中新增程式庫檔目錄

<?php
// 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;
?>

Example 2-5. 手動設定 SMARTY_DIR 常數變數

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

Now that the library files are in place, it's time to setup the Smarty directories for your application. Smarty requires four directories which are (by default) named templates, templates_c, configs and cache. Each of these are definable by the Smarty class properties $template_dir, $compile_dir, $config_dir, and $cache_dir respectively. It is highly recommended that you setup a separate set of these directories for each application that will use Smarty.

在找出程式庫檔的位置之後,接下來就要在你所撰寫的應用程式中設定 Smarty 的目錄。 在預設情況下 Smarty 需要四個目錄且命名為 templates, templates_c, configscache。目錄的名稱可以根據 Smarty 的以下四個變數名稱 $template_dir, $compile_dir, $config_dir, 與 $cache_dir 的不同設定而更換。我們強烈建議當你在使用 Smarty 作為樣板引擎實作你的應用程式時,請 將上述四個變數指向不同的目錄。

Be sure you know the location of your web server document root. In our example, the document root is /web/www.example.com/docs/. The Smarty directories are only accessed by the Smarty library and never accessed directly by the web browser. Therefore to avoid any security concerns, it is recommended to place these directories outside of the document root.

請先確認你已經了解你網站伺服器根目錄的位置。在我們的範例中,根目錄的位置在 /web/www.example.com/docs/。Smarty 的目錄只會被 Smarty 的程式庫檔使用而不會被網頁瀏覽器直接使用,如此一來就可以 避免安全性的問題,所以建議將這些目錄放在根目錄 之外

For our installation example, we will be setting up the Smarty environment for a guest book application. We picked an application only for the purpose of a directory naming convention. You can use the same environment for any application, just replace "guestbook" with the name of your app. We'll place our Smarty directories under /web/www.example.com/smarty/guestbook/.

以我們的安裝為例,我們將 Smarty 應用在一個留言版的應用程式,根據命名習慣與 程式的功能增加了一個新的目錄。你也可以在別的應用程式使用相同的 Smarty 環境設定,在我 們的範例中,我們將 Smarty 的目錄放在 /web/www.example.com/smarty/guestbook/ 路徑下。

You will need as least one file under your document root, and that is the script accessed by the web browser. We will call our script index.php, and place it in a subdirectory under the document root called /guestbook/.

在你的網頁文件根目錄中最少需要一個檔案,而這個檔案是由網頁瀏覽器所取得的, 我們通常把這個檔案命名為 index.php,且將這個檔案放在 /guestbook/ 的次目錄中。

Technical Note: It is convenient to setup the web server so that "index.php" can be identified as the default directory index, so if you access "http://www.example.com/guestbook/", the index.php script will be executed without "index.php" in the URL. In Apache you can set this up by adding "index.php" onto the end of your DirectoryIndex setting (separate each entry with a space.)

技術提醒: 我們可以經由網頁伺服器將 "index.php" 設定為預設檔案,當我們輸入網址 "http://www.example.com/guestbook/" 時,index.php 就會自動執行而不用在 URL 中輸入 "index.php"。在 Apache 中你可以在你的 DirectoryIndex 設定後面增加 "index.php" (DirectoryIndex 與 index.php 之間要用空白鍵隔開)。

Lets take a look at the file structure so far:

讓我們先查看一下到目前為止的檔案結構:

Example 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/internals/*.php
/usr/local/lib/php/Smarty/plugins/*.php

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

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

Smarty will need write access to the $compile_dir and $cache_dir, so be sure the web server user can write to them. This is usually user "nobody" and group "nobody". For OS X users, the default is user "www" and group "www". If you are using Apache, you can look in your httpd.conf file (usually in "/usr/local/apache/conf/") to see what user and group are being used.

Smarty 將會寫入資料到 $compile_dir$cache_dir,所以請確定網頁伺服器使用者擁有可以寫入這兩個目錄的權利。此時的使用者通常是群組為 "nobody" 的使用者 "nobody",而 OS X 的使用者預設設定為群組是 "www" 的使用者 "www"。 如果你使用 Apache 作為你的網頁伺服器,你可以檢視你的 httpd.conf 檔案 (通常路徑為 "/usr/local/apache/conf/")看執行網頁的群組與使用者為何。

Example 2-7. 設定檔案的存取權限

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

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

Technical Note: chmod 770 will be fairly tight security, it only allows user "nobody" and group "nobody" read/write access to the directories. If you would like to open up read access to anyone (mostly for your own convenience of viewing these files), you can use 775 instead.

技術提醒: chmod 770 是太過嚴格的存取權限設定,如此一來僅同意 "nobody" 使用者與 "nobody" 群 組取出與寫入目錄。如果你想將存取權限開放給所有人(可以隨意的瀏覽檔案)的話,你可以設 定成 775。

We need to create the index.tpl file that Smarty will load. This will be located in your $template_dir.

我們需要建立一個 index.tpl 檔案以供 Smarty 載入,且會根據你的電腦中 $template_dir 的值決定載入位置。

Example 2-8. 編輯 /web/www.example.com/smarty/guestbook/templates/index.tpl

{* Smarty *}

Hello, {$name}!

Technical Note: {* Smarty *} is a template comment. It is not required, but it is good practice to start all your template files with this comment. It makes the file easy to recognize regardless of the file extension. For example, text editors could recognize the file and turn on special syntax highlighting.

技術提醒: {* Smarty *} 是一個樣板註解,不是必須的,但是在樣板檔案開始前使用這個註解是一個 良好的習慣。使用註解的好處在於不管檔案將如何擴大,我們都可以容易的辨別檔案。舉例來說 ,文字編輯器可以容易的辨別出檔案結構,且用顏色強調出特別的語法。

Now lets edit index.php. We'll create an instance of Smarty, assign a template variable and display the index.tpl file. In our example environment, "/usr/local/lib/php/Smarty" is in our include_path. Be sure you do the same, or use absolute paths.

一開始先編輯 index.php,我們將建立一個 Smarty 的實例,設定一個樣板變數且將 變數的值放入 index.tpl。在我們的示範環境中 "/usr/local/lib/php/Smarty" 包含在我們的 include_path 路徑中。請確定你的環境中設定的值也是一樣的,如果設定值不一樣的話請使用 絕對路徑。

Example 2-9. 編輯 /web/www.example.com/docs/guestbook/index.php

<?php

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

$smarty = new Smarty;

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

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

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

Technical Note: In our example, we are setting absolute paths to all of the Smarty directories. If /web/www.example.com/smarty/guestbook/ is within your PHP include_path, then these settings are not necessary. However, it is more efficient and (from experience) less error-prone to set them to absolute paths. This ensures that Smarty is getting files from the directories you intended.

技術提醒: 在我們的範例中,我們都是使用絕對路徑,如果 /web/www.example.com/smarty/guestbook/ 在你的 include_path 設定中的話,就沒必要非得使用絕對路徑不可,然而,使用絕對路徑的 好處在於程式執行時將更有效率(經驗法則得知),且更不容易出錯,因為使用絕對路徑將確保 Smarty 會從你想要讀取檔案的目錄中讀取檔案。

Now load the index.php file from your web browser. You should see "Hello, Ned!"

現在請從網頁伺服器中載入 index.php,你將會看到 "Hello, Ned!"

You have completed the basic setup for Smarty!

到目前為止你已經完成 Smarty 最基本的設定了!