您的位置:首页技术文章
文章详情页

smarty3.0以上配置 Smarty模板引擎

【字号: 日期:2022-06-09 13:34:09浏览:3作者:猪猪
/**file:init.inc.phpSmarty对象的实例化及初使化文件*/

define("ROOT",str_replace("\\","/",dirname(__FILE__))."/"); //指定项目的根路径

//********smarty**********

/*推荐使用Smarty3以上版本方式设置默认的路径,设置成功后都返回$smarty对象本身,可以使用连贯操作*/

$smarty=newSmarty(); //实例化Smarty类的对象$smarty

$smarty->setTemplateDir(ROOT."templates/") //设置所有模板文件存放的目录

//->addTemplateDir(ROOT."templates2/") //可以添加多个模板目录(前后台各一个)

->setCompileDir(ROOT."templates_c/") //设置所有编译过的模板文件存放的目录

->setPluginsDir(ROOT."plugins/") //设置为模板扩充插件存放的目录

->setCacheDir(ROOT."cache/") //设置缓存文件存放的目录

->setConfigDir(ROOT."configs"); //设置模板配置文件存放的目录

$smarty->caching=false; //设置Smarty缓存开关功能

$smarty->cache_lifetime=60*60*24; //设置模板缓存有效时间段的长度为1天

$smarty->left_delimiter="<{"; //设置模板语言中的左结束符

$smarty->right_delimiter="}>"; //设置模板语言中的右结束符


Smarty2时的设置方式:

$smarty->template_dir="./templates"; //设置模板目录,2.0设置方法,3.0沿用但不推荐

$smarty->compile_dir="./templates_c"; //设置编译目录,2.0设置方法,3.0沿用但不推荐

$smarty->config_dir="./configs/"; //设置配置目录,2.0设置方法,3.0沿用但不推荐

$smarty->cache_dir="./cache/"; //设置缓存目录,2.0设置方法,3.0沿用但不推荐


Smary在3.0中对属性进行了封装。可以使用如下方法进行访问获得目录。

$smarty->getCacheDir(); //得到当前缓存目录路径

$smarty->getTemplateDir(); //得到当前模板目录路径的数组

$smarty->getConfigDir(); //得到当前配置目录路径

$smarty->getCompileDir(); //得到当前编译目录路径

$smarty->getPluginsDir(); //得到当前插件目录路径数组同样用下面的方法进行目录设置:


#设置新的模板目录,注意设置后模板目录的数组只有该值一个,不管原来有几个值

$smarty->setTemplateDir("./templates/");

$smarty->setCompileDir("./templates_c/"); //设置新的编译目录

$smarty->setConfigDir("./configs/"); //设置新的配置目录

$smarty->setCacheDir("./cache/"); //设置新的缓存目录


//引用的模板文件的路径必须在模板目录数组中,否则报错,由于仍然用原来的模板文件,这样模板数组中有两个路径。

$smarty->addTemplateDir("./templates2/"); //添加一个新的插件目录,如果用set将取消插件数组,变为单指

$smarty->addPluginsDir("./myplugins");


<?phprequire_once"smarty/Smarty.class.php"; //包含Smarty类的文件

$smarty=newSmarty(); //创建Smarty类对象

$smarty->setTemplateDir("system/templates"); //设置模板存放目录

$smarty->setCompileDir("system/templates_c"); //设置编译过的模板文件存放目录

$smarty->setCacheDir("system/cache"); //设置存放Smarty缓存文件目录

$smarty->setConfigDir("system/config"); //设置模板中特殊配置文件存放的目录

//$smarty->caching=true; //设置开启Smarty缓存模板功能

//$smarty->cache_lifetime=60*60*24; //设置模板缓存有效时间段长度为1天

$smarty->debugging=true; //调试页面发生错误会显示错误在页面上false则不显示

$smarty->left_delimiter="<{"; //设置模板语言左结束符

$smarty->right_delimiter="}>"; //设置模板语言右结束符?>
标签: PHP smarty
相关文章: