<?php
/**
* 另一条模板和代码分离的路
*
* 声明:这只是一个模板分离类,不算模板引擎
*
* 可以安全的使用在你的mvc中
* <code>
* <li>支持模板和模型、控制器分离</li>
* <li>支持缓存</li>
* </code>
* @link http://lib.cublog.cn
* @author 流水孟春 cmpan(at)qq.com
* @copyright GPL2
*/
class View {
public $debug = false;
public $caching = false;
public $forceCaching = false;
public $expire = 900;
protected $_vars = array();
protected $_tpl = '';
protected $_path = 'templates/';
protected $_cacheId = null;
protected $_cacheDir = 'caches/';
protected $_cached = false;
public function __set($key, $val) {
if(!isset($this->_vars[$key])) $this->_vars[$key] = $val;
}
public function __get($key) {
if(isset($this->_vars[$key])) return $this->_vars[$key];
else return $this->debug ? '{undefined}' : null;
}
/**
* 设置模板所在目录
*
* @param string $path
*/
public function setPath($path){
if(!is_string($path)) throw new Exception("path $path did not a string"); // 参数类型不是String,提示并退出
$path = str_replace('\\', '/', $path);
$path = rtrim($path, '/') . '/';
if(!file_exists($path)) throw new Exception("path $path dose not exists");
$this->_path = $path;
}
/**
* 设置模板变量
*
* @param string $name
* @param mixed $value
*/
public function set($name, $value) {
$this->_vars[$name] = $value;
}
/**
* 批量填充模板变量
*
* @param mixed $vars
* @param bool $clear
*/
public function setVars($vars, $clear = false) {
if($clear) {
$this->_vars = $vars;
}
else {
if(is_array($vars)) $this->_vars = array_merge($this->_vars, $vars);
}
}
/**
* 显示内容
*
* @param string $tpl
*/
public function display($tpl) {
#使用静态缓存的情况
if ($this->caching) {
print $this->fetchCache($tpl);
}
# 不使用静态缓存的情况
else {
print $this->fetch($tpl);
}
}
/**
* 获取模板解析后的内容
*
* @param string $tpl
* @return string
*/
public function fetch($tpl) {
if(!is_string($tpl)) throw new Exception("file $tpl did not a string");
$tpl = str_replace('\\', '/', $tpl);
$tpl = ltrim($tpl, '/');
extract($this->_vars);
ob_start();
require_once $this->_path . $tpl;
$contents .= ob_get_contents();
ob_end_clean();
return $contents;
}
/**
* 设置缓存目录
*
* @param string $path
*/
public function setCacheDir($path) {
if (!is_string($path)) throw new Exception("path $path did not a string.");
$path = str_replace('\\', '/', $path);
$path = ltrim($path, '/');
if(!file_exists($path)) throw new Exception("path $path dose not exists");
$this->_cacheDir = $path;
}
/**
* 设置cache的ID
*
* @param string $cacheId
*/
public function setCacheId($tpl, $cacheId = null){
$this->_cacheId = $this->_cacheDir . "$tpl." .sprintf('%X', crc32($cacheId)) . '.php';
}
/**
* 是否已经缓存
*
* @param string $tpl 模板文件
* @return bool
*/
public function isCached($tpl) {
if (!$this->_cacheId) {
$this->_cacheId = $this->_cacheDir . $tpl . '.php';
}
# Force Caching?
if ($this->forceCaching) return false;
# Has Bean Cached?
if($this->_cached) return true;
# Passed a cacheId?
if(!$this->_cacheId) return false;
# Cache file exists?
if(!file_exists($this->_cacheId)) return false;
# Can get the time of the file?
if(!($mtime = filemtime($this->_cacheId))) return false;
# Cache expired?
if(($mtime + $this->expire) < time()) {
@unlink($this->_cacheId);
return false;
}
else {
$this->_cached = true;
return true;
}
}
/**
* 获取缓存内容
*
* @param string $tpl
* @return string
*/
public function fetchCache($tpl) {
if(!is_string($tpl)) throw new Exception("file $tpl did not a string");
$tpl = str_replace('\\', '/', $tpl);
$tpl = ltrim($tpl, '/');
if($this->isCached($tpl)) {
if(!$contents = @file_get_contents($this->_cacheId))
throw new Exception('Unable to read cache:' . $this->_cacheId);
return $contents;
}
else {
$contents = $this->fetch($tpl);
$contents = "<?php exit; ?>\n" . $contents;
# Write the cache
if (!@file_put_contents($this->_cacheId, $contents))
throw new Exception('Unable to write cache:' . $this->_cacheId);
return $contents;
}
}
}
附件中有使用的例子,和phplib/smarty的思想很不一样哦,