搜索
首页php教程PHP源码php超实用的模板引擎

php超实用的模板引擎

May 25, 2016 pm 05:13 PM
php

方法: 

$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;,//加密因子
);

[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 );
		}
	}
}

                   


                   

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用