Skip to content

Instantly share code, notes, and snippets.

@chzyer
Last active December 10, 2015 22:39
Show Gist options
  • Save chzyer/4504171 to your computer and use it in GitHub Desktop.
Save chzyer/4504171 to your computer and use it in GitHub Desktop.
Codeigniter的模板函数

Codeigniter的模板函数

写策划写数据库结构写公共类写了一天,codeigniter的框架总体还不错,只是没有把htm代码和php代码分开是一大失误,框架类产品本身的价值在于统一了格式,方便统一代码而适合多人合作写代码,而php代码与htm代码分开则避免网页美工和后台程序代码堆在一起,有利于他人修改和阅读,discuz就是采用这种模式,如果要分开,必须实现htm文件能执行php代码,看上去是不可能,但其实很简单,于是,我为codeigniter写了一个template函数,思路为,先读取模板文件(htm)的内容,存入变量,用正则将特定格式的代码替换成真正的php代码,并另存为php代码文件并include进来,就是说,把我们原本应该亲手做的事推给代码做,我们只需写html代码,这个函数已写完!当然,此思路参考自discuz,哈哈­

codeigniter官方说,PHP伪代码会带来性能的下降,与CI的快捷简便原理想冲突,我想,当把template的文件有效期限设置长一点,因为本身大型的网站修改不是很频繁,

// --------------------------------------------------------------------

/**
 * 模板机制
 *
 * 该函数可以先将htm模板文件转换成PHP的文件
 *
 */ 
// --------------------------------------------------------------------
function template($file){
	$views_folder = "views";
	$template_folder = "template";
	$cachetime = 3;
	$tplfile=$file;
	//------------------------------------------------路径处理
	//自动加上后缀名
	if (basename($file) == basename($file, '.htm')) {
		$tplfile.=".htm";
	}else{
		$file = basename($tplfile,'.htm');
	}
	
	//如果根目录不存在该文件则到common文件夹下查找!方便引用common内的文件
	if (!@$fp = fopen(APPPATH.$views_folder."/".$template_folder."/".$tplfile,"r")) {
		$file='common/'.$file;
		$tplfile='common/'.$tplfile;
	}
	$cachefile = BASEPATH."template/".str_replace("/","_",$file).".tpl.php";
	
	if (@$fp = fopen($cachefile,"r") && time() - filemtime($cachefile) < $cachetime){//已经存在了,并且时间未过,包括进来
	
	}else{
		//----------------------如果缓存文件不存在或者过期则写入新文件 
		$tplfile=APPPATH.$views_folder."/".$template_folder."/".$tplfile;
		
		if (!@$fp = fopen($tplfile,"r")){
			echo "Cannot open template file \"{$tplfile}\" , by [system/libraries/loader.php]";
			return 0;
		}
		$template = @fread($fp, filesize($tplfile));
		fclose($fp);
		//处理文件
		$template = preg_replace("/{loop\\s+?(\\$\\S*)\\s+?(\\$\\S*)\\s*?}.*\\n/","< ?if(is_array(\\1)){foreach (\\1 as \\2){?>",$template);
		$template = preg_replace("/{loop\\s+?(\\$\\S*)\\s+?(\\$\\S*)\\s+?(\\$\\S*)\\s*?}.*\\n/","< ?if(is_array(\\1)){foreach (\\1 as \\2 => \\3){?>",$template);
		$template = preg_replace("/{\/loop}/","< ?}}?>",$template);
		$template = preg_replace("/{template\\b\\s+(.*)\\b\\s*?}/",'< ?include $this->load->template("\\1");?>',$template);
		$template = preg_replace("/{eval\\s+(.*?)}/",'< ?\\1;?>',$template);
		$template = preg_replace("/{(\\$\S+?)}/","< ?=\\1?>",$template);
		$template = preg_replace("/{if\\s+(.*?)\\s*?}/","< ?if (\\1) {?>",$template); 
		$template = preg_replace("/{elseif\\s+(.*?)\\s*?}/","< ?}elseif (\\1) {?>",$template);
		$template = preg_replace("/{else}/","< ?}else{?>",$template);
		$template = preg_replace("/{\/if}/","< ?}?>",$template);
		//处理文件结束
		if(!@$fp = fopen($cachefile, 'w')) {
			echo "Cannot write template file \"{$cachefile}\" , by [system/libraries/loader.php]";
			return 0;
		}
		flock($fp, 2);
		$template="< ?if(!defined('BASEPATH'))exit('No direct script access allowed');?>".$template;
		fwrite($fp, $template);
		fclose($fp);
	} 
	return $cachefile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment