Home > php教程 > PHP源码 > body text

php超实用的模板引擎

PHP中文网
Release: 2016-05-25 17:13:56
Original
1143 people have browsed it

方法: 

$this->assign('style',$style);//变量
$this->display();//模板 
<?php
/*配制*/
$config=array(
/* 数据库设置 */
&#39;DB_TYPE&#39;               => &#39;mysql&#39;,     // 数据库类型
&#39;DB_HOST&#39;               => &#39;localhost&#39;, // 服务器地址
&#39;DB_NAME&#39;               => &#39;php&#39;,          // 数据库名
&#39;DB_USER&#39;               => &#39;root&#39;,      // 用户名
&#39;DB_PWD&#39;                => &#39;123&#39;,          // 密码
&#39;DB_PREFIX&#39;             => &#39;jiaodu_&#39;,    // 数据库表前缀
&#39;DB_CHARSET&#39;            => &#39;utf8&#39;,      // 数据库编码默认采用utf8
/* SESSION设置 */
&#39;SESSION_START&#39;  => &#39;user&#39;,    //session方式,文件方式:file, 数据库设置为user
/* 模板引擎设置 */
&#39;TMPL_ADMIN_PATH&#39;  =>&#39;admin&#39;,//后台目录名称
&#39;TMPL_COMPILE_PATH&#39;  =>&#39;/Runtime&#39;,//读写目录
&#39;TMPL_PATH&#39;  =>&#39;/template&#39;,//模板路径
&#39;TMPL_TEMPLATE_SUFFIX&#39;  => &#39;html&#39;,     // 默认模板文件后缀
&#39;TMPL_L_DELIM&#39;          => &#39;{&#39;,  // 模板引擎普通标签开始标记
&#39;TMPL_R_DELIM&#39;          => &#39;}&#39;,  // 模板引擎普通标签结束标记
&#39;TMPL_STRIP_SPACE&#39;      => true,       // 是否去除模板文件里面的html空格与换行
&#39;TMPL_CACHE_ON&#39;  => true,        // 是否开启模板编译缓存,设为false则每次都会重新编译,一般用于模板调试
/* URL设置 */
&#39;URL_HTML_SUFFIX&#39;       => &#39;html&#39;,  // URL伪静态后缀设置
&#39;URL_PATHINFO_MODEL&#39;    => 2,       // URL模式,1不隐藏、2隐藏入口文件[需要规则支持]
/*其它设置*/
&#39;PASS_ENCRYPT&#39;  =>&#39;www.php.cn&#39;,//加密因子
);
Copy after login

[PHP]代码

<?php
/**
 * 模板解析类
 * @author 角度 QQ:1286522207
 *
 */
class template extends Action{
	private $config;
	private $CompileDir;//编译目录
	private $templateDir;//模板目录
	private $templateFile;
	private $debuy=1;//是否调试
	private $assign;//变量
	public function __construct($templateFile){
		$this->config();
		$this->templateFile=$templateFile;
	}
	private function config(){
		global $config;
		$this->config=$config;
		$this->CompileDir=$this->config[&#39;TMPL_COMPILE_PATH&#39;].&#39;/Compile&#39;;
		$this->templateDir=$this->config[&#39;TMPL_PATH&#39;];
		$this->debuy=$this->config[&#39;TMPL_CACHE_ON&#39;];

	}

	/**
	 * 检查编译目录
	 */
	public function is_CompileDir(){
		$dir=APP_PATH.$this->CompileDir;
		if (!is_dir($dir)){
			if (!mkdir($dir)){
				die(&#39;编译目录自动创建失败,请手动创建&#39;);
			}
		}
		if (!is_writeable($dir)){
			die(&#39;编译目录没有写入权&#39;);
		}
	}
	/**
	 * 注入变量
	 */
	public function assign($assign) {
		$this->assign=$assign;
	}
	/**
	 * 输出模板
	 */
	public function display(){
		$this->is_CompileDir();
		$this->CompileCheck();
	}
	/**
	 * 检查编译
	 */
	private  function CompileCheck(){
		$this->is_CompileDir();
		$filename=APP_PATH.$this->CompileDir.&#39;/&#39;.md5($this->templateFile).&#39;.php&#39;;
		if ($this->debuy || !is_file($filename)){
			$this->tmplstrtpspace($filename);
		}
		foreach ($this->assign as $key=>$row){
			$$key=$row;
		}
		include $filename;
	}
	/**
	 * 格式化模板并写入编译
	 */
	private  function tmplstrtpspace($filename){
		if ($this->config[&#39;TMPL_STRIP_SPACE&#39;]){
			$find     = array("~>\s+<~","~>(\s+\n|\r)~");
			$replace  = array("><",">");
			$tmplContent = preg_replace($find, $replace,$this->templateCheck());
		}else {
			$tmplContent = $this->templateCheck();
		}
		if (file_put_contents($filename,trim($tmplContent))){
			return true;
		}else {
			die(&#39;编译写入失败&#39;);
		}
	}
	/**
	 * 检查模板
	 */
	private  function templateCheck(){
		$PATH=APP_PATH.$this->templateDir.&#39;/&#39;.$this->templateFile.&#39;.html&#39;;
		if (is_file($PATH)){
			return $this->template_compile(file_get_contents ( $PATH ));
		}else {
			die(&#39;模板:&#39;.$this->templateFile.&#39;.html 不存在&#39;);
		}
	}
	/**
	 * 编译模板
	 */
	private function template_compile($template_Conver){
		if (empty($template_Conver)){
			return $template_Conver;
		}else {
			$_Left= $this->config[&#39;TMPL_L_DELIM&#39;];
			$_Right= $this->config[&#39;TMPL_R_DELIM&#39;];
			$template_Preg [] = &#39;/<\?(=|php|)(.+?)\?>/is&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;(else if|elseif) (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;for (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;while (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;(loop|foreach) (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;if (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;else&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . "(eval|_)( |[\r\n])(.*?)" . $_Right . &#39;/is&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;_e (.*?)&#39; . $_Right . &#39;/is&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;_p (.*?)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;\/(if|for|loop|foreach|eval|while)&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;((( *(\+\+|--) *)*?(([_a-zA-Z][\w]*\(.*?\))|\$((\w+)((\[|\()(\&#39;|")?\$*\w*(\&#39;|")?(\)|\]))*((->)?\$?(\w*)(\((\&#39;|")?(.*?)(\&#39;|")?\)|))){0,})( *\.?[^ \.]*? *)*?){1,})&#39; . $_Right . &#39;/i&#39;;
			$template_Preg [] = "/(	| ){0,}(\r\n){1,}\";/";
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;(\#|\*)(.*?)(\#|\*)&#39; . $_Right . &#39;/&#39;;
			$template_Preg [] = &#39;/&#39; . $_Left . &#39;\%([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)&#39; . $_Right . &#39;/&#39;;
			$template_Replace [] = &#39;&lt;?\\1\\2?&gt;&#39;;
			$template_Replace [] = &#39;<?php }else if (\\2){ ?>&#39;;
			$template_Replace [] = &#39;<?php for (\\1) { ?>&#39;;
			$template_Replace [] = &#39;<?php while (\\1) { ?>&#39;;
			$template_Replace [] = &#39;<?php foreach ((array)\\2) { $__i++; ?>&#39;;
			$template_Replace [] = &#39;<?php if (\\1){ ?>&#39;;
			$template_Replace [] = &#39;<?php }else{ ?>&#39;;
			$template_Replace [] = &#39;<?php \\3; ?>&#39;;
			$template_Replace [] = &#39;<?php echo \\1; ?>&#39;;
			$template_Replace [] = &#39;<?php print_r(\\1); ?>&#39;;
			$template_Replace [] = &#39;<?php } ?>&#39;;
			$template_Replace [] = &#39;<?php echo \\1;?>&#39;;
			$template_Replace [] = &#39;&#39;;
			$template_Replace [] = &#39;&#39;;
			$template_Replace [] = &#39;<?php echo $this->lang_array[\&#39;\\1\&#39;];?>&#39;;
			return preg_replace ( $template_Preg, $template_Replace, $template_Conver );
		}
	}
}
Copy after login

                   


                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!