-
vendor目录放置扩展包
新建目录/vendor/Smarty-3.1.7
下载smarty,并将smarty中libs放到/vendor/Smarty-3.1.7下
Paste_Image.png -
设置该路径常量
入口文件index.php
Paste_Image.png
Paste_Image.png - 新建Csmarty.php对smarty进行封装,需要新建编译目录
application/libraries/Csmarty.php
application/views/templates_c
Paste_Image.png - Cmarty.php
require_once VENDORPATH . "Smarty-3.1.7/libs/Smarty.class.php";
class Csmarty
{
private $smarty;
public function __construct($templatePath = '', $param = array())
{
if (empty($templatePath)) {
$templatePath = APPPATH . 'views/';
}
$this->smarty = new Smarty();
$this->smarty->left_delimiter = "{{";
$this->smarty->right_delimiter = "}}";
$this->smarty->compile_check = true;
$this->smarty->caching = false;
//$this->smarty->escape_html = true;
$this->smarty->template_dir = $templatePath;
$this->smarty->compile_dir = $templatePath . 'templates_c' . DS;
$this->smarty->loadFilter("variable", "htmlspecialchars");
foreach ($param as $key => $val) {
$this->smarty->$key = $val;
}
}
public function assignRaw($var, $value)
{
$this->smarty->assign($var, $value);
}
............
- controller调用
class Saa extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('csmarty');
}
public function index()
{
$this->csmarty->assignRaw('test','333');
$this->csmarty->display('smarty.html');
}
}
views/smarty.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{$test}}
11111111
</body>
</html>
网友评论