Smarty学习1——安装和配置

来源:互联网 发布:开根号的算法 编辑:程序博客网 时间:2024/06/16 09:34

以我的项目目录参考,安装和配置都在/index.php文件下进行
请使用Smarty3版本

1、项目目录

/    demo/        templates/            index.tpl        templates_c/        configs/        cache/            index.php    libs/        ……    index.php//主文件

2、引入Smarty类文件,实例化

require_once( '/libs/Smarty.class.php' );$smarty = new Smarty;

一般来说,如果你的程序可以找到Smarty.class.php文件, 那么你不需要设置SMARTY_DIR, Smarty会自行进行赋值。 然而,如果Smarty.class.php文件不在你的 include_path内, 或者你不能在程序中使用绝对路径的时候,那么你需要定义SMARTY_DIR。 SMARTY_DIR 必须以斜杠(/)结尾。

3、配置Smarty

  • Smarty可配置四个目录,默认名称分别是 templates/, templates_c/, configs/ 和 cache/
    强烈建议分别在每个使用Smarty的程序中都单独定义这些目录
$pro_dir = dirname( __FILE__ );    // 项目根目录$smarty->setTemplateDir( $pro_dir . '/demo/templates/' );$smarty->setCompileDir( $pro_dir . '/demo/templates_c/' );$smarty->setConfigDir( $pro_dir . '/demo/configs/' );$smarty->setCacheDir( $pro_dir . '/demo/cache/' );

你可以通过testInstall() 来测试Smarty是否有权限读写这些目录。

$smarty->testInstall();

打印信息

Smarty Installation test...Testing template directory...D:\WWW\demo\smarty\demo\templates is OK.Testing compile directory...D:\WWW\demo\smarty\demo\templates_c is OK.Testing plugins directory...D:\WWW\demo\smarty\libs\plugins is OK.Testing cache directory...D:\WWW\demo\smarty\demo\cache is OK.Testing configs directory...D:\WWW\demo\smarty\demo\configs\ is OK.Testing sysplugin files...... OKTesting plugin files...... OKTests complete.

4、基础配置

//$smarty->force_compile = true;   // 强迫编译,用于调试//$smarty->debugging = true;       // debug调试$smarty->caching = true;           //开启缓存$smarty->cache_lifetime = 120;     //缓存存活时间(秒)
0 0
原创粉丝点击