Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

基础安装

在Smarty�行包中,目录/libs/ 里�的文件就是Smarty的库文件。 库文件都是.php文件,开�者�应该编辑它们。 库文件�以多个应用程�共用,而且�在�级新版本的Smarty时进行覆盖。

下�的例�Smarty的库文件被解压:

  • /usr/local/lib/Smarty-v.e.r/ 在 *nix 的环境

  • 以å�Š c:\webroot\libs\Smarty-v.e.r\ 在 windows 环境.

Example 2.1. 载入Smarty库文件


Smarty-v.e.r/
   libs/
      Smarty.class.php
      debug.tpl
      sysplugins/* (everything)
      plugins/*    (everything)

    

Smarty的SMARTY_DIR 常�,是定义 Smarty库文件libs/目录的 完整系统路径。 一般�说,如果你的程��以找到Smarty.class.php文件, 那么你�需�设置SMARTY_DIR, Smarty会自行进行赋值。 然而,如果Smarty.class.php文件�在你的 include_path内, 或者你�能在程�中使用�对路径的时候,那么你需�定义SMARTY_DIR。 SMARTY_DIR 必须以斜�(/)结尾。.

在PHP中实例化Smarty对象的方法:


<?php
// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
?>

    

�行上�的程�。如果�生错误�示说找�到Smarty.class.php文件, 那么你需�按下�的其中一�方法�处�:

Example 2.2. 手动指定 SMARTY_DIR 常�


<?php
// *nix style (note capital 'S')
define('SMARTY_DIR', '/usr/local/lib/Smarty-v.e.r/libs/');

// windows style
define('SMARTY_DIR', 'c:/webroot/libs/Smarty-v.e.r/libs/');

// hack version example that works on both *nix and windows
// Smarty is assumend to be in 'includes/' dir under current script
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/');

require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
?>

    

Example 2.3. 用�对路径引用库文件


<?php
// *nix style (note capital 'S')
require_once('/usr/local/lib/Smarty-v.e.r/libs/Smarty.class.php');

// windows style
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');

$smarty = new Smarty();
?>

    

Example 2.4. 将库文件路径增加到php.ini 文件


;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; *nix: "/path1:/path2"
include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/"

; Windows: "\path1;\path2"
include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"


Example 2.5. 通过函数ini_set() �设置include path


<?php
// *nix
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'/usr/local/lib/Smarty-v.e.r/libs/');

// windows
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'c:/webroot/lib/Smarty-v.e.r/libs/');
?>

    

现在,库文件已�放好了,�以开始为你的程��置Smarty了:

  • Smartyå�¯é…�置四个目录,默认å��称分别是 templates/, templates_c/, configs/ å’Œ cache/。

  • 这些都分别对应Smarty类的属性定义 $template_dir, $compile_dir, $config_dir, å’Œ $cache_dir。

  • 强烈建议分别在æ¯�个使用Smarty的程åº�中都å�•独定义这些目录。

  • ä½ å�¯ä»¥é€šè¿‡testInstall() æ�¥æµ‹è¯•Smarty是å�¦æœ‰æ�ƒé™�读写这些目录。

在下�的安装例�中,我们将为一个留言本程�建立Smarty环境。 我们�供了一个目录命�约定的例�。 你�以为任何的程�建立�样的环境,仅需�修改guestbook/�称。

Example 2.6. 目录文件结构


/usr/local/lib/Smarty-v.e.r/libs/
        Smarty.class.php
        debug.tpl
        sysplugins/*
        plugins/*

/web/www.example.com/
        guestbook/
        templates/
            index.tpl
        templates_c/
        configs/
        cache/
        htdocs/
            index.php

    

明确你的web�务器文档根目录。在下�的例�中, 文档根目录是/web/www.example.com/guestbook/htdocs/。 Smarty目录仅�以通过Smarty库文件访问,而�能直接被�览器访问。 这样�以��一些安全问题,强烈建议(但�强制)把这些目录 放到WEB�务器文档根目录之外。

将会有至少一个文件是放到文档根目录的,这个文件也会被�览器访问到。 我们将这文件命�为index.php, 放置到文档根目录/htdocs/中。

Smarty需�一些对目录的 读写�� (windows用户请忽略),包括 $compile_dir 和 $cache_dir 目录 (templates_c/ 和 cache/), 所以,�确�web�务器用户有��读写它们。

Note

通常是用户“nobody� 和组“nobody�. OS X用户,默认用户是“www�和组“www�. 如果你使用Apache,你�以看看你的httpd.conf �确定是使用什么用户和组的。

Example 2.7. 设置目录的读写��


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

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

    

说明

chmod 770是比较适当的安全设置,仅�许 用户“nobody�和组“nobody��以读写访问这些目录。 如果你希望能让所有人都能读�该目录 (大部分是因为你需�方便查看这些目录内的文件), 你�以使用775。

我们需�创建文件index.tpl,然�供Smarty显示。 文件需�放到 $template_dir目录内。

Example 2.8. /web/www.example.com/guestbook/templates/index.tpl


{* Smarty *}

Hello {$name}, welcome to Smarty!

    

技术说明

{* Smarty *}是模�的 注释. 虽然�是必须的,但在模�内添加注释这是个很好的习惯。 它�以帮助识别出文件类型,而�需�看�缀。 比如说,代�编辑器�以识别该文件并自动语法高亮。

现在,我们�修改index.php. 我们将创建Smarty的实例,给模�assign() 赋值��, 并且display()显示该模�文件 index.tpl。

Example 2.9. 修改 /web/www.example.com/docs/guestbook/index.php


<?php

require_once(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty();

$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');

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

//** un-comment the following line to show the debug console
//$smarty->debugging = true;

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

?>

    

说明

在我们的例�中,我们为Smarty的目录使用了�对路径。 如果/web/www.example.com/guestbook/在你PHP的include_path内, 那么,这些设置�是必须的。 然而,设置��对路径,是更高效和更�容易出错(�自�验)。 这�以��Smarty的目录路径被设置�正确的。

现在,用�览器访问index.php文件。 你�以看到"Hello Ned, welcome to Smarty!"

你已�完�了Smarty的基础安装�

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 4 plus 19? (Are you human?)

Advertisement