现在的位置: 首页 > 综合 > 正文

使用自己的Smarty类

2018年08月08日 ⁄ 综合 ⁄ 共 1046字 ⁄ 字号 评论关闭
 利用Smarty的扩展设置(Extended Setup),通过扩展类和初始化Smarty环境,来安装Smarty。我们可以通过一种方法来取代重复设置目录路径、给相同的参数赋值。我在project目录下建立了include文件夹,并在其中建立setup.php。(其实命名为smarty.lib.php更合适,它的目的是配置调用smarty库)

建立文件setup.php
<?php
// setup.php文件是个放置读取必需应用库文件的好地方
// 你可以像这样做,例如:
// require_once('project/include/setup.php');

class Smarty_w01f extends Smarty {

 function Smarty_w01f()
 {
 // Class Constructor.
 // 构造类,我用工作室给他命名:D
 // These automatically get set with each new instance.
 // 这些自动开始准备每一个实例

 $this->Smarty();
 $this->template_dir = './project/templates/';
 $this->compile_dir = './project/templates_c/';
 $this->config_dir = './project/configs/';
 $this->cache_dir = './project/cache/';
    
  // 如果你想修改Smarty的调用语法为{**},下面就可以
  //$this->left_delimiter = '{*';
  //$this->right_delimiter = '*}';

 $this->caching = true;
 }
}
?>


随之我要修改index.php
<?php
/*
file name:index.php
*/
define('SMARTY_DIR',str_replace("","/",getcwd()).'./libs/smarty/');
// 注意:Smarty有一个大写字母“S”
require_once(SMARTY_DIR . 'Smarty.class.php');
require_once('project/include/setup.php');

$smarty = new Smarty_w01f;

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

?>

抱歉!评论已关闭.