My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* DsTemplate 模板类
*
* @filesource DsTemplate.class.php
* @package DsTemplate
* @subpackage
* @version $id: 0.2, utf8, Fri Jan 1 15:30:11 CST 2010
* @author LD King <kldscs[at]gmail.com>
* @copyright Copyleft (D.) 2007 - 2009 MiFunny China Inc.
* @link http://mifunny.info/
* @see example: testView.php
*/
class DsTemplate{
private static $_instance;
//private $_tp = null; //模板根目录
//private $_content = null; //最后显示的内容

/**
* 变量数组
* TEMPLATEPATH 模板根目录
*
* @var array
*/
private $_vars = array(
'TEMPLATEPATH' => null,
);

/**
* 初始化 模板引擎
*
* @param string 模板根目录
*/
public function __construct($template_path = null){
if( is_string($template_path) ){
$this->_vars['TEMPLATEPATH'] = $template_path;
}
self::$_instance = $this;
}

/**
* 重写魔法方法 __set 和 __get
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value){
$this->_vars[$key] = $value;
}

public function __get($key){
return $this->_vars[$key];
}

/**
* 获得最后的显示信息
*
* @param string $template 模板文件全名
*/
public function fetch($template){
//目录分隔符
if( !defined('DS') ) define('DS', DIRECTORY_SEPARATOR);
//模板根目录
if( !defined('TP') ) define('TP', empty($this->_vars['TEMPLATEPATH']) ? '' : rtrim($this->_vars['TEMPLATEPATH'], '\\/').DS );

$template = TP.str_replace('/', DS, $template);
if( is_file($template) ){
unset( $this->_vars['TEMPLATEPATH'] );
extract($this->_vars, EXTR_OVERWRITE);

$errno = error_reporting();
error_reporting($errno & ~E_NOTICE);
ob_start();
include($template);
$content = ob_get_contents();
ob_end_clean();
error_reporting($errno);

return $content;
}else{
throw new LogicException('('.__CLASS__.'->'.__METHOD__.')'.'Error: '.$template.' is not a file !');
return false;
}
}

public function display($template){
echo $this->fetch($template);
}

/**
* 获得静态视图对像
*
* @return object DsTemplate
*/
public static function getInstance(){
if( !(self::$_instance instanceof self) ){
new self(); // self::$_instance = new self();
}
return self::$_instance;
}

/**
* 静态方法, 显示视图
*
* @param string $template 模板文件
* @param array $vars 变量数组
*/
public static function show($template, array $vars=null){
$view = self::getInstance();
//$view->TEMPLATEPATH = $template_path;
if( !empty($vars) ){
$view->_vars = $vars;
}
$view->display($template);
}

}//END class DsTemplate

/**
* 显示变量? stripslashes
*
* @param mixed $value 需显示的变量, 字符串 or 数字
* @param bool $escape 是否转义
*/
function _e($value, $escape=true){
if($escape and is_string($value) ){
echo htmlspecialchars($value, ENT_QUOTES);
}else{
echo $value;
}
}//END func _e
?>

Change log

r47 by kldscs on Jan 7, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r46 by kldscs on Jan 2, 2010   Diff
也许可以更简单
r45 by kldscs on Jan 2, 2010   Diff
perfect
r44 by kldscs on Jan 1, 2010   Diff
这叫暴强!
All revisions of this file

File info

Size: 3333 bytes, 132 lines
Powered by Google Project Hosting