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发行版在/libs/目录里的库文件(就是解压了). 这些php文件你可不能乱画哦.这些文件被所有应用程序共享,也只能在你升级到新版的smarty的时候得到更新。

Example 2-1. Smarty library files
Smarty手册范例 2-1.Smarty库文件

/core/*.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.class.php文件,你不需要设置SMARTY_DIR,Smarty将会自己运作。但是,如果 Smarty.class.php没有在你的include_path(php.ini里的一项设置)里,或者没有在你的应用程序里设置它的绝对路径的时候,你就必须手动配置SMARTY_DIR 了(大多数程序都如此)SMARTY_DIR必须包含结尾斜杠。

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

Example 2-2. Create Smarty instance of Smarty
Smarty手册范例 2-2.创建Smarty实例

$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 文件"的错误时,你应该这样做:

Example 2-3. Supply absolute path to library file
Smarty手册范例 2-3.加入库文件目录的绝对路径

$smarty = new Smarty;

Example 2-4. Add library directory to php_include path
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:
$smarty = new Smarty;

Example 2-5. Set SMARTY_DIR constant manually
Smarty手册范例 2-5.手工设置SMARTY_DIR常量

$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要求4个目录,默认下命名为:tempalates, templates_c, configs and cache。每个都是可以自定义的,可以修改Smarty类属性: $template_dir, $compile_dir, $config_dir, and $cache_dir respectively。强烈推荐你为每个用到smarty的应用程序设置单一的目录!

Be sure you know the location of your web server document root. In our example, the document root is "/web/". 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服务器文件根目录。在我们的例子里,文件根目录是:"/web/"Smarty的4个目录 只可以被那些库文件访问,不可以被网络上的浏览器访问的目录。因此为避免任何安全问题,要求将那4个目录和网页文件目录(就是浏览器看的)分开来。

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/".
在我们的安装例子里,我们将为一个留言板程序配置smarty环境。我们挑选应用程序只为了实现目录命名约定。你可以对任何程序使用相同的环境,只要将"guestbook"改成你要的名字就可以了。我们将把Smarty目录放在 "/web/"下。

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 "", 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.)
技术提示:建立web服务器很方便,这个文件可以被web服务器自动识别。如果你访问"",你不需要在URL上输入"index.php",index.php脚本就可以被执行。在Apache服务器中,可以通过在DirectoryIndex的后面添加"index.php" 文件(用反斜杠分开每个入口)来完成设置。

Lets take a look at the file structure so far:

Example 2-6. Example file structure
Smarty手册范例 2-6.例子的文件结构




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必须可写。通常是user "nobody" 和 group "nobody"。如果是 OSX用户,默认为user "web" 和 group "web"。如果你在使用Apache,你可以看看httpd.conf 文件 (通常在"/usr/local/apache/conf/"目录下)哪些user和group正在被使用。

Example 2-7. Setting file permissions
Smarty手册范例 2-7 文件权限设置

chown nobody:nobody /web/
chmod 770 /web/

chown nobody:nobody /web/
chmod 770 /web/

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相当安全了,它只让user "nobody" 和 group "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. Editing /web/
Smarty手册范例 2-8 编辑/web/

{* 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. Editing /web/
Smarty手册范例 2-9.编辑/web/

// load Smarty library

$smarty = new Smarty;

$smarty->template_dir = '/web/';
$smarty->compile_dir = '/web/';
$smarty->config_dir = '/web/';
$smarty->cache_dir = '/web/';



Technical Note: In our example, we are setting absolute paths to all of the Smarty directories. If '/web/' 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.

在我们的例子里,已经设置了所有Smarty目录的绝对目录。如果 '/web/' 已经包括在 include_path里了,那么这些设置则没有必要。但是,从经验和通用性看来,为避免发生错误,还是配置一下为好。

Now load the index.php file from your web browser. You should see "Hello, Ned!"
现在在浏览器打开 index.php,你应该看到"Hello, Porky!"

You have completed the basic setup for Smarty!