<table cellspacing="0" cellpadding="0" width="100%" summary="Header navigation table" border="0"><tbody><tr><th align="middle" colspan="3">Smarty - the compiling PHP template engine</th></tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="what.is.smarty.html">Prev</a></td> <td valign="bottom" align="middle" width="50%"/> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="smarty.for.designers.html">Next</a></td></tr></tbody></table>
# Chapter 2. Installation
**第二章.****安装**
**Table of Contents**[Requirements](#)要求 [Basic Installation](#)基本安装[Extended Setup](#)扩展设置
# Requirements 要求
Smarty requires a web server running PHP 4.0.6 or later.
Smarty要求web服务器运行php4.0.6和以上版本.
# 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.
安装Smarty很简单,只须将Smarty库文件放在/libs/目录里就行了(就是解压了), 你可不能修改这些php文件哦。这些文件被所有应用程序共享,也只能在你升级到新版的smarty的时候得到更新。
- /usr/local/lib/Smarty-v.e.r/ for *nix machines (linux、unix类系统 )
- and c:\webroot\libs\Smarty-v.e.r\ for the windows environment.(windows环境)
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <a name="AEN70"> </a> <b>Example 2.1. Required Smarty library files<br/> 例 2-1.所需的Smarty库文件</b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><p>Smarty-v.e.r/<br/> libs/<br/> Smarty.class.php<br/> debug.tpl<br/> sysplugins/* (everything)<br/> plugins/* (everything)</p> </td> </tr></tbody></table></div></td> </tr></tbody></table>
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.
Here is how you create an instance of Smarty in your PHP scripts:
Smarty使用一个名为'[SMARTY_DIR](#)'的php常量作为它的系统库目录。基本上,如果你的应用程序可以找到*Smarty.class.php*文件,你不需要设置SMARTY_DIR,Smarty将会自己运作。但是,如果 *Smarty.class.php*没有在你的include_path(php.ini里的一项设置)里,或者没有在你的应用程序里设置它的绝对路径的时候,你就必须手动配置SMARTY_DIR了(大多数程序都如此)SMARTY_DIR必须包含结尾斜杠(‘/’)。
这里是你在你的php脚本里创建一个smarty的应用实例的例子:
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN"><?php// NOTE: Smarty has a capital 'S'require_once('Smarty.class.php');$smarty = new Smarty();?></pre></td> </tr></tbody></table></div></td> </tr></tbody></table>
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文件"*的错误时,你应该这样做:
**Example 2.2. Set SMARTY_DIR constant manually
****例 2-2.手动设置SMARTY_DIR常量**
|
~~~
<?php// *nix style (note capital 'S')define('SMARTY_DIR', '/usr/local/lib/Smarty-v.e.r/libs/');// windows styledefine('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 scriptdefine('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/')require_once(SMARTY_DIR . 'Smarty.class.php');$smarty = new Smarty();?>
~~~
|
|-----|
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"><b>Example 2-3. Supply absolute path to library file<br/> 例 2-3.加入库文件目录的绝对路径</b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN"><?php// *nix style (note capital 'S')require_once('/usr/local/lib/Smarty-v.e.r/libs/Smarty.class.php');// windows stylerequire_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');$smarty = new Smarty();?></pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <p> <b>Example 2-4. Add library directory to php_include path<br/> 例 2-4.在include_path加入库文件目录</b> </p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN">;;;;;;;;;;;;;;;;;;;;;;;;;; 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\"</pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <p> <b><span class="SCREEN">Example 2.5. Appending the include path in a php script with ini_set()</span><br/> 例 2-5.php脚本中使用in_set()设置加载路径</b></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN">[http://php.net/ini-set]<?php// *nixini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'/usr/local/lib/Smarty.class.php'));// windowsini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'c:/webroot/lib/Smarty.class.php'));?></pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
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.
- You can verify if your system has the correct access rights for these directories with testInstall().
现在库文件已经搞定,该是设置为你的应用程序配置其他有关Smarty的目录的时候了。
- Smarty要求4个目录,默认下命名为:*tempalates*, *templates_c*, *configs* and *cache*
。 - 每个都是可以自定义的,可以分别修改Smarty类属性: *[$template_dir](#)*, *[$compile_dir](#)*,[*$config_dir*](#)和*[$cache_dir](#)*。
- 强烈推荐你为每个用到smarty的应用程序设置单一的目录(如同Smarty安装包里的demo的文件结构)!
- 可以使用[testInstall()](#)函数核实你的系统是否拥有这些目录存取权限。
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 application.
在我们的安装例子里,我们将为一个留言板程序配置smarty环境。我们挑选应用程序只为了实现目录命名约定。你可以对任何程序使用相同的环境,只要"guestbook/"改成你要的名字就可以了。
**Example 2.6. What the file structure looks like
例 2-5.文件结构示例**
<table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><dd><td><p> /usr/local/lib/Smarty-v.e.r/libs/<br/></p><dd>Smarty.class.php<br/> debug.tpl<br/> sysplugins/*<br/> plugins/*</dd> <p><br/> /web/www.example.com/<br/></p><dd>guestbook/<br/> --templates/<br/> ----index.tpl<br/> --templates_c/<br/> --configs/<br/> --cache/<br/> --htdocs/<br/> ----index.php</dd> </td></dd> </tr></tbody></table>
Be sure you know the location of your web server document root. In our example, the document root is "/web/www.mydomain.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.
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/".
Smarty will need write access (windows users please ignore) to the $compile_dir and $cache_dir directories (templates_c/ and cache/), so be sure the web server user account can write to them.
确定你已经知道了你的web服务器文件根目录。在我们的例子里,文件根目录是:"/web/www.mydomain.com/docs/"。Smarty的4个目录(libs下的)只可以被那些库文件访问,不可以被网络上的浏览器访问的目录。因此为避免任何安全问题,要求将那4个目录和网页文件目录(就是浏览器看的)分开来。
在你的文档目录下至少得有一个文件,这个文件可以被浏览器访问,我们叫它 "index.php"好了,把它放到"/guestbook/"目录下。
Smarty的$compile_dir和$cache_dir目录(templates_c/ and cache/)需要写权限(windows用户请忽略),才能确保web服务器用户帐户可以写入。
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> 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 to see what user and group are being used.<br/> 通常情况下,用户和组都为“nobody”。对于苹果OS X系统用户,用户和组默认为“www”。如果使用Apache服务器,可以通过查看httpd.conf文件(通常在"/usr/local/apache/conf/"目录下)确认用户和组。</td> </tr></table>
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <p> <b><span class="SCREEN">Example 2.7. Permissions and making directories writable</span><br/> 例 2-7.权限和目录可写设置</b> </p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN">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/</pre></td> </tr></tbody></table></div></td> </tr></tbody></table>
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><p>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. </p> <p> chmod 770相当安全了,它只让user "nobody" 和 group "nobody" 读/写 访问。如果你要对任何人开放读取访问权限(大多是为了你自己查看文件),你可以使用 775。 </p></td> </tr></table>
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目录里。
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <p> <b>Example 2-8. Editing /web/www.mydomain.com/smarty/guestbook/templates/index.tpl<br/> 例 2-8 编辑/web/www.mydomain.com/smarty/templates/index.tpl</b> </p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN">{* Smarty *}
Hello, {$name}!</pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p>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. </p> <p> {* Smarty *} 是一个模板注释。虽然并不是必须的,但是这可以很好的锻炼你在模板文件里加入注释的习惯。它可以使文件便于识别。例如,一些文本编辑器可以识别这个文件,并加以语法高亮显示。 </p></td> </tr></table>
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里了。
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"> <p> <b>Example 2-9. Editing /web/www.mydomain.com/docs/guestbook/index.php<br/> 例 2-9.编辑/web/www.mydomain.com/docs/guestbook/index.php</b> </p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN"><?phprequire_once(SMARTY_DIR . 'Smarty.class.php');$smarty = new Smarty();$smarty->template_dir = '/web/www.example.com/guestbook/templates/';$smarty->compile_dir = '/web/www.example.com/guestbook/templates_c/';$smarty->config_dir = '/web/www.example.com/guestbook/configs/';$smarty->cache_dir = '/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');?>
</pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p>Technical Note: In our example, we are setting absolute paths to all of the Smarty directories. If '/web/www.mydomain.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. </p> <p> 在我们的例子里,已经设置了所有Smarty目录的绝对目录。如果 '/web/www.mydomain.com/smarty/guestbook/' 已经包括在 include_path里了,那么这些设置则没有必要。但是,从经验和通用性看来,为避免发生错误,还是配置一下为好。 </p></td> </tr></table>
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><p>Note<br/> In our example, we are setting absolute paths to all of the Smarty directories. If /web/www.example.com/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.</p> <p>在我们的例子中,我们为所有Smarty目录设置了绝对路径。如果/web/www.example.com/guestbook/包含在你的php变量include_path里面,那么这样做不是必须的。然而,设置绝对路径会更有效率且(从经验上说)可以减少错误。这样可以确保Smarty能从你期望的目录中取得文件!</p></td> </tr></table>
Now load the index.php file from your web browser. You should see "Hello Ned, welcome to Smarty!"
You have completed the basic setup for Smarty!
现在在浏览器打开 index.php,你应该看到"Hello Ned, welcome to Smarty!"
你现在已经完成了Smarty的基本设置,恭喜!!
# Extended Setup
扩展设置
This is a continuation of the basic installation, please read that first!
A slightly more flexible way to setup Smarty is to extend the class [http://php.net/ref.classobj] and initialize your Smarty environment. So instead of repeatedly setting directory paths, assigning the same vars, etc., we can do that in one place.
Lets create a new directory /php/includes/guestbook/ and make a new file called setup.php.In our example environment, /php/includes is in our include_path. Be sure you set this up too, or use absolute file paths.
这是基本安装的延续,请先阅读上一节!
一个稍微更具灵活性的安装方法是扩展类并初始化Smarty环境。这样我们可以避免重复地配置路径、设置变量...只需在一个文件里就可以搞掂Smarty安装。
我们创建一个目录"/php/includes/guestbook/",建立一个"setup.php"文件。下例环境中,"/php/includes"是include_path相对路径,请先确定是否配置,或者也可以使用一个绝对路径。
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"><b>Example 2-10. /php/includes/guestbook/setup.php<br/></b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN">// load Smarty library
require('Smarty.class.php');
// The setup.php file is a good place to load
// required application library files, and you setup.php文件是一个很好的加载应用程序的类库文件
// can do that right here. An example: 你也可以这样做,例如:
// require('guestbook/guestbook.lib.php');
class Smarty_GuestBook extends Smarty {
function Smarty_GuestBook() {
// Class Constructor. These automatically get set with each new instance.
//类构造函数.创建实例的时候自动配置
$this->Smarty();
$this->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
$this->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
$this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$this->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
$this->caching = true;
$this->assign('app_name','Guest Book');
}
}</pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
Now lets alter the index.php file to use setup.php:
现在我们通过setup.php文件更改一下index.php文件
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td><div class="EXAMPLE"><b>Example 2.11. /web/www.example.com/guestbook/htdocs/index.php</b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="SCREEN"><?phprequire('guestbook/setup.php');$smarty = new Smarty_GuestBook();$smarty->assign('name','Ned');$smarty->display('index.tpl');?></pre> </td> </tr></tbody></table></div></td> </tr></tbody></table>
Now you see it is quite simple to bring up an instance of Smarty, just use Smarty_GuestBook() which automatically initializes everything for our application.
现在你看到创建一个smarty实例有多么的简单,只需使用Smarty_GuestBook()就可以自动初始化我们的应用程序。
<table cellspacing="0" cellpadding="0" width="100%" summary="Footer navigation table" border="0"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="what.is.smarty.html">Prev</a></td> <td valign="top" align="middle" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="smarty.for.designers.html">Next</a></td></tr><tr><td valign="top" align="left" width="33%">What is Smarty?<br/> 什么是Smarty?</td> <td valign="top" align="middle" width="34%"><a accesskey="U" href="getting.started.html">Up</a></td> <td valign="top" align="right" width="33%">Basic Installation<br/> 基本安装</td></tr></tbody></table>
- Smarty模板编译引擎
- 序
- 译序
- I.开始
- 第一章. 什么是Smarty?
- 第二章. 安装
- II.模板设计者篇
- 第三章.基本语法
- 注释
- 变量
- 函数
- 属性
- 双引号里嵌入变量
- 数学运算
- 忽略Smarty解析
- 第四章.变量
- 从PHP分配的变量
- 从配置文件读取的变量
- 变量范围
- {$smarty}保留变量
- 第五章.变量调节器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.组合修改器
- 第七章.内置函数
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定义函数
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.调试控制台
- III.模板程序员篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty类变量
- $template_dir
- 第十三章.Smarty类方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.缓存
- 建立缓存
- 多重缓存
- 缓存集合
- 控制插件输出的可缓存性
- 第十五章.高级特性
- 安全
- 通过模板更改设置
- 模板继承
- 数据流
- 对象
- 静态类
- 预过滤器
- 后过滤器
- 输出过滤器
- 缓存处理函数
- 资源
- 第十六章.以插件扩展Smarty
- 插件如何工作
- 命名约定
- 编写插件
- 模板函数
- 调节器
- 块函数
- 编译函数
- 预滤器/后滤器
- 输出过滤器
- 资源
- 插入
- Ⅳ.附录
- 第十七章.疑难解答
- Smarty/PHP 错误
- 第十八章.使用技巧和经验
- 空白变量处理
- 默认变量处理
- 传递变量标题给头模板
- 日期
- WAP/WML
- 组件化模板
- 拒绝电子邮件地址
- 第十九章. 相关资源
- 第二十章. 漏洞
- 3.0安装包
- 2.x版本升级至3.x版本的提示
- 3.0.x使用指南
- 翻译人员列表