- class Templates{
- /*获取站点根目录*/
- var $web_root;
- /*模板目录*/
- var $templates_dir;
- /*编译目录*/
- var $compile_dir;
- /*通过它判断读取的是前台模板还是后台模板*/
- var $templates_postion;
- /*临时存储模板文件内容*/
- var $out_put_content;
- /*临时数组*/
- var $_array=array();
- var $left_tag='[';
- var $right_tag=']';
- /*
- *其它类对象
- */
- var $class_obj;
- /*
- *构造函数初始化数据
- */
- function __construct($v)
- {
- $this->web_root=$_SERVER['DOCUMENT_ROOT'].'/';
- $this->templates_postion=$v;
- }
- /*
- 读取XML文件获取相应的模板与编译的目录地址
- */
- function read_site_config_xml()
- {
- /*
- 定义读取XML
- $xml_dom;
- */
- $xml_dom=simplexml_load_file($this->web_root.'web.config.xml');
- $this->compile_dir=$this->web_root.$xml_dom->compileConfig->dir;
- switch($this->templates_postion)
- {
- case 'front':
- $this->templates_dir=$this->web_root.$xml_dom->templatesConfig->templatesDir;
- break;
- case 'admin':
- $this->templates_dir=$this->web_root.$xml_dom->templatesConfig->adminTemplatesDir;
- $this->compile_dir.='admin/';
- break;
- }
- unset($xml_dom);
- }
- /*
- 读取模板文件
- */
- function read_template($templatename)
- {
- $this->read_site_config_xml();
- return file_get_contents($this->templates_dir.$templatename);
- }
- /*
- 注册模板变量
- */
- function assign($tag,$tagvalue)
- {
- if(is_array($tag))
- {
- foreach($tag as $tagkey=>$tagkeyvalue)
- {
- $this->_array[$tagkey]=$tagkeyvalue;
- }
- }
- else
- {
- $this->_array[$tag]=$tagvalue;
- }
- }
- /*
- 向模板中赋值
- */
- function assgin_value()
- {
- foreach($this->_array as $tag=>$tagvalue)
- {
- if(is_array($tagvalue))
- {
- $this->out_put_content=str_replace($this->left_tag.$tag.$this->right_tag,'$this->_array[\\''.$tag.'\\']',$this->out_put_content);
- }
- else
- {
- $this->out_put_content=str_replace($this->left_tag.$tag.$this->right_tag,'$this->_array[\\''.$tag.'\\']',$this->out_put_content);
- }
- }
- }
- /*
- 输出模板文件
- */
- function out_put($templatename)
- {
- //先检查此文件是否已编译,如果编译则直接输出
- if($this->check_compile($templatename))
- {
- echo $this->out_put_content;
- }
- else
- {
- $this->out_put_content=$this->read_template($templatename);
- $this->convert_php_tag();
- $this->assgin_value();
- $this->make_compile($templatename);
- echo $this->out_put_content;
- }
- }
- /*
- 编译模板
- */
- function make_compile($compilename)
- {
- $name=$this->compile_dir.$compilename.'.php';
- file_put_contents($name,$this->out_put_content,LOCK_EX);
- ob_start();
- include($name);
- $this->out_put_content=ob_get_contents();
- ob_end_clean();
- }
- /*
- 检查编译
- */
- function check_compile($compilename)
- {
- $this->read_site_config_xml();
- $name=$this->compile_dir.$compilename.'.php';
- if(file_exists($name))
- {
- ob_start();
- include($name);
- $this->out_put_content=ob_get_contents();
- ob_end_clean();
- $returnvalue=true;
- }
- else
- {
- $returnvalue=false;
- }
- return $returnvalue;
- }
- /*
- 处理模板文件中PHP语法
- */
- function convert_php_tag()
- {
- $html_file_tag=array
- (
- /*结构控制语句开始、结束标签*/
- '<{foreach' => '<?php foreach',
- '<{if' => '<?php if',
- '<{for'=>'<?php for',
- ':}>'=>': ?>',
- '<{/if}>'=>'<?php endif; ?>',
- '<{/else}>'=>'<?php else: ?>',
- '<{/foreach}>'=>'<?php endforeach; ?>',
- '<{/for}>'=>'<?php endfor; ?>',
- /*输出数组变量内容标签*/
- '<${'=>'<?php echo ',
- '}$>'=>'; ?>',
- /*加载文件标签*/
- '</{'=>'<?php include($this->web_root.\\'',
- '}/>'=>'\\'); ?>',
- '<{'=>'<?php ',
- '}>'=>' ;?>'
- );
- foreach($html_file_tag as $key=>$keyvalue)
- {
- $this->out_put_content=str_replace($key,$keyvalue,$this->out_put_content);
- }
- }
- /*
- *引用其它类文件
- */
- function include_class($classname)
- {
- @include($this->web_root.'core/'.$classname.'.php');
- $this->class_obj=new $classname();
- }
- /*
- 释放资源
- */
- function __destruct()
- {
- unset($this->out_put_content);
- unset($this->_array);
- unset($this->class_obj);
- }
- }
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/100920135772.html
来源: http://www.codesnippet.cn/detail/100920135772.html