搜尋
首頁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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中