您的位置:首页 > 其它

smarty学习之旅(二)

2016-04-05 11:12 246 查看
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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: