swoole_framework /libs/system/SwooleTemplate.php

Language PHP Lines 219
MD5 Hash 25dd0d567dc674d5811ec1b21f9aa958
Repository https://github.com/monkeycraps/swoole_framework.git View Raw File
  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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * Swoole模板工具类
 * 提供一个简单的模板系统,支持标签、模板数据库映射查询,遍历,显示等功能
 * @author Tianfeng.Han
 * @package SwooleSystem
 * @subpackage template
 *
 */
class SwooleTemplate extends ArrayObject
{
	public $templates_dir;
	
	public $in_content='';
	public $out_content='';
	public $c_content='';
		
	public $pattern;
	
	public $db;
	
	public $fields;
	public $fields_full;
	
	const FILTER_DIR = '/taglib/filter/';
	
	function __construct($db)
	{
		$this->db = $db;
	}
	
	function parse($tpl_name)
	{
		
		$filename = WEBPATH.$this->templates_dir.$tpl_name;
		$this->c_content = $this->in_content = file_get_contents($filename);
		
		$tags=array();
		$this->pattern = "#".preg_quote('{{','~')."[^}]+".preg_quote('}}','~')."#i";
        preg_match_all($this->pattern,$this->in_content,$tags,PREG_PATTERN_ORDER);		
        $tags=$tags[0];
        
		foreach($tags as $tag)
		{
			$tagname=self::trim($tag);
			$exp=explode(":",$tagname);
			if(count($exp)>2) die("错误的标签:".$tag);
			if(count($exp)==1) continue;
			$this->parse_static($tag);
			call_user_func_array(array($this,'proc_'.$exp[0]),array(trim($exp[1]),$tag));
		}
		$this->end();
		echo $this->out_content;
	}

	function assign($key,$value)
	{
		$this->$key=$value;
	}
	
	function proc_var($tag,$tag_head)
	{
		$this->c_content = str_replace($tag_head,$this->$tag,$this->c_content);

//		$params=explode(".",$varname);
//		$varnum=count($params);
//		if($varnum==1)
//		{
//			return $$varname;
//		}
//		elseif($varnum==2)
//		{
//			$arrayname=$params[0];
//			$array=$$arrayname;
//			return $array[$params[1]];
//		}
	}
	
	public static function parse_param($tagbody)
	{
		$rs = array();
		$list= explode(" ",$tagbody);
		foreach($list as $val)
		{
			$arr=explode("=",$val,2);
			$rs[$arr[0]]=$arr[1];
		}
		return $rs;
	}
	
	function proc_records($tag,$tag_head)
	{
		$param = self::parse_param($tag);
		$select = new SelectDB($this->db);
		$select->put($param);
		$body = $this->get_body('records');
		$list = $select->getall();
		
		$content = '';
		
		foreach($list as $record)
		{
			$content.=$this->parse_record_body($body,$record);
		}
		$this->out_content.=$content;
		$this->clear('records');
	}
	
	function proc_record($tag,$tag_head)
	{
		$param = self::parse_param($tag);
		$record_name = $param['_name'];
		$record_field = $param['_field'];
		if($this->$record_name!=false) $record=$this->$record_name;
		else
		{
			$select = new SelectDB($this->db);
			$select->put($param);
			$record = $select->getone();
			$this->$record_name = $record;
		}
		$this->out_content.=$record[$record_field];
		$this->c_content = str_replace($tag_head,'',$this->c_content);
	}
	
	function parse_record_body($line,$record)
	{
		$vals = array();
		preg_match_all($this->pattern,$line,$vals);
		$vals=$vals[0];
		foreach($vals as $val) $line=str_replace($val,$record[self::trim($val)],$line);
		return $line;
	}
	
	function clear($tagname)
	{
		$end_tag = '{{/'.$tagname.'}}';
		$pos_e = mb_strpos($this->c_content,$end_tag);		
		$this->c_content = substr($this->c_content,$pos_e+strlen($end_tag),-1);
	}
	
	function parse_static($tag)
	{
		$pos = strpos($this->c_content,$tag);
		$this->out_content.= substr($this->c_content,0,$pos);
		$this->c_content = substr($this->c_content,$pos,strlen($this->c_content)-$pos);
	}
	
	function end()
	{
		$this->out_content.=$this->c_content;
	}
	
	function get_body($tagname)
	{
		$end_tag = '{{/'.$tagname.'}}';
		$pos_s = mb_strpos($this->c_content,'}}')+2;
		$pos_e = mb_strpos($this->c_content,$end_tag);
		return mb_substr($this->c_content,$pos_s,$pos_e-$pos_s);
	}
	
	function trim($string)
	{
		return str_replace('}}',"",str_replace('{{',"",$string));
	}
	
	function get_fields($body)
	{
		preg_match_all('#{([^\}^|]+)[}|]#i',$body,$vals);
		$this->fields = array_unique($vals[1]);
		
		preg_match_all('#{([^\}]+)}#i',$body,$vals);
		
		$this->fields_full = $vals[1];
		return $this->fields;
	}
	
	function parse_loop($data,$body)
	{
		$content = '';
		if(empty($this->fields_full))
			$this->get_fields($body);
				
		foreach($data as $record)
		{
			$line = $body;
			
			foreach($this->fields_full as $field)
			{
				$this->filter($line,$field,$record);
			}
			$content.=$line;
		}
		return $content;
	}
	
	function filter(&$line,$field,&$record)
	{
		$f = explode('|',$field);
		$field_name = $f[0];
		unset($f[0]);
		$value = $record[$field_name];
		foreach($f as $filter)
		{
			$f_params = explode(':',$filter);
			self::_load_filter($f_params[0]);
			$func = 'swoole_filter_'.$f_params[0];
			$f_params[0] = $field_name;
			$value = $func($value,$f_params,$record);
		}
		$line = str_replace('{'.$field.'}',$value,$line);
	}
	
	static function _load_filter($filter_name)
	{
		require_once(LIBPATH.self::FILTER_DIR.$filter_name.'.filter.php');
	}
}
?>
Back to Top