smarty学习之旅(二)

Smarty库文件

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

创建Smarty实例

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

设置Smarty库文件的一些方法

1.加入库文件目录的绝对路径
require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty;
Example 2-4. Add library directory to php_include path
2.在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:
require('Smarty.class.php');
$smarty = new Smarty;
Example 2-5. Set SMARTY_DIR constant manually
3.手工设置SMARTY_DIR常量
define('SMARTY_DIR','/usr/local/lib/php/Smarty/');
require(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty;
库文件已经搞定,该是设置为你的应用程序配置其他有关Smarty的目录的时候了。Smarty要求4个目录,默认下命名为:tempalates, templates_c, configs and cache。每个都是可以自定义的,可以修改Smarty类属性: $template_dir, $compile_dir, $config_dir, and $cache_dir respectively。强烈推荐你为每个用到smarty的应用程序设置单一的目录。

确定你已经知道了你的web服务器文件根目录。在我们的例子里,文件根目录是:"/locolhost/docs/"Smarty的4个目录 只可以被那些库文件访问,不可以被网络上的浏览器访问的目录。因此为避免任何安全问题,要求将那4个目录和网页文件目录(就是浏览器看的)分开来。对于可被访问的目录,我们把目录名定为guestbook。你可以对任何程序使用相同的环境,只要将"guestbook"改成你要的名字就可以了。我们将把Smarty目录放在 "/locolhost/smarty/guestbook/"下。

技术提示:建立web服务器很方便,这个文件可以被web服务器自动识别。如果你访问”http://www.mydomain.com/guestbook/“,你不需要在URL上输入”index.php”,index.php脚本就可以被执行。在Apache服务器中,可以通过在DirectoryIndex的后面添加”index.php” 文件(用反斜杠分开每个入口)来完成设置。
文件结构设置如下:

/localhost/Smarty/Smarty.class.php
/localhost/lib/php/Smarty/Smarty_Compiler.class.php
/localhost/lib/php/Smarty/Config_File.class.php
/localhost/lib/php/Smarty/debug.tpl
/localhost/lib/php/Smarty/core/*.php
/localhost/lib/php/Smarty/plugins/*.php

/localhost/smarty/guestbook/templates/
/localhost/smarty/guestbook/templates_c/
/localhost/smarty/guestbook/configs/
/localhost/smarty/guestbook/cache/

/localhost/docs/guestbook/index.php

文件权限设置
chown nobody:nobody /localhost/smarty/guestbook/templates_c/
chmod 770 /localhost/smarty/guestbook/templates_c/

chown nobody:nobody /localhost/smarty/guestbook/cache/
chmod 770 /localhost/smarty/guestbook/cache/

文件启动设置

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

$smarty = new Smarty;

$smarty->template_dir = '/localhost/smarty/guestbook/templates/';
$smarty->compile_dir = '/localhost/smarty/guestbook/templates_c/';
$smarty->config_dir = '/localhost/smarty/guestbook/configs/';
$smarty->cache_dir = '/localhost/smarty/guestbook/cache/';

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

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

php拓展类,将启动和配置smarty封装起来实现

require('Smarty.class.php');

// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');是一个很好的加载应用程序的类库文件(就是扩展类)
//例如你可以在index文件里包含它

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');
 }

}

启动方式

require('guestbook/setup.php');

$smarty = new Smarty_GuestBook;

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

$smarty->display('index.tpl');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值