当前位置:首页> PHP教程> php模板
关键字
文章内容
Smarty的安装
 
 
修改时间:[2010/12/12 07:17]    阅读次数:[2746]    发表者:[起缘]
 
smarty模板可以在本站下载/download.php?page=2

1.1Smarty环境要求
Smarty要求web服务器运行php4.0.6或以上版本。
1.2Smarty基本安装
Smarty库文件放到/libs/目录中 ,这些文件不需要编译,被所有的应用程序共享。
必需要有的库文件
Smarty.class.php,Smarty_Compiler.class.php,Config_File.class.php,Debug.tpl
/internals/*.php (internals内的所有文件),/plugins/*.php (根据自己网页需要可以选定一些,当然都包括是没错的)

Smarty使用一个叫做SMARTY_DIR的php常量作为它的系统库目录。基本上,如果你的应用程序可以找到Smarty.class.php文件,则你不需要设置SMARTY_DIR,Smarty将自己运作。可是,如果Smarty.class.php不在你的include_path(php.ini中)路径中或是你不能为你的应用程序提供Smarty.class.php的一个绝对路径,那么你必须手动的设置SMARTY_DIR。SMARTY_DIR必须包含结尾斜杠。

下面给大家列举一个在PHP 脚本内生成Smarty的实例:
<?php
    
require_once(“Smarty.class.php”);
    
$smarty=new Smarty();
?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
运行上面的脚本,如果得到了一个错误:Smarty.class.php文件找不到,则你必须按下面的方法做:
方法一:手动定义SMARTY_DIR常量
<?php
    define
(“SMARTY_DIR”,”库文件的目录”);
    require_once(
SMARTY_DIR.'Smarty.class.php');
    
$smarty=new Smarty();
?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
方法二:提供库文件的绝对目录
<?php
    
Require_once(“…………/libs/Smarty-v.e.r/libs/Smarty.class.php”);
    
$smarty=new Smarty();
?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
方法三:添加库目录到PHP inlcude_path
<?php
    
require_once(“Smarty.class.php”);
    
$smarty=new Smarty();
?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
总之你必须通过一种方法让你的程序找到Smarty.class.php

到现在你已经让你的程序找到了库文件,下面是为你的程序设置其它的Smarty目录的时候了。
Smarty要求有四个目录,它们的默认名分别为'templates/','templates_c/','configs/','cache/'
它们都可以通过修改Smarty类的$template_dir,$compile_dir,$config_dir,$cache_dir属性重新定义。
[attach]1687[/attach]
强烈推荐为每个用到Smarty的应用程序设置单一的目录。如果不明白这句话就看下面图:
[attach]1688[/attach]
确定你已经知道了Web服务器的文档根目录,在我们的例子里,文档根目录是/Easy Smarty/docs。Smarty目录只可以被Smarty库访问而绝不可以被web浏览器访问(当然放到一起也没啥),为了避免这种情况的发上,我们可以把Smarty目录和网页文件所在的目录分开。
接下来我们给出一个小例子来告诉你目录的命名规范。无论什么程序你都可以相同的环境,只需把GuestBook用你的程序名来替换。我们把我们的Smarty目录放到/Easy Smarty下。
在你的文档目录docs内至少要有一个文件可以被浏览器访问,我们将叫它index.php并且把它放到/GuestBook下。

下面来看一下/Easy Smart下的文件结构:(网站的根目录为Easy Smarty)
/SmartyLibs/libs/internals/*.php
/SmartyLibs /libs/plugins/*.php
/SmartyLibs /libs/Smarty.class.php
/SmartyLibs /libs/Smarty_compiler.class.php
/SmartyLibs /libs/Config_File.class.php
/SmartyLibs /libs/debug.tpl

/Smarty/GuestBook/templates/
/Smarty/GuestBook/templates_c/
/Smarty/GuestBook/configs/
/Smarty/GuestBook/cache/

/docs/GuestBook/index.php
[attach]1689[/attach]
我们需要创建index.tpl文件让Smarty载入,这个文件要放到$template_dir(如果不修改Smarty.class.php)则为templates文件夹。

编辑/smarty/GuestBook/templates/index.tpl

{* Smarty *}
Hello {$name},welcome to Smarty!

{* Smarty *}是一个模板注释。这不是必需的,但是这可以很好的锻炼你在模板文件里加入注释的*惯。它可以使文件便于识别。很多文本编辑器对该注释提供高亮。

编辑Easy Smarty/docs/GuestBook/index.php
<?php
    
require_once(SMARTY_DIR,'../../Smarty.class.php');
    
$smarty=new Smarty();
    
$smarty->template_dir=' ../../smarty/GuestBook/templates/';
    
$smarty->compile_dir=' ../../smarty/GuestBook/templates_c/';
    
$smarty->config_dir=' ../../smarty/GuestBook/configs/';
    
$smarty->cache_dir=' ../../smarty/GuestBook/cache/';

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


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)