search
Homephp教程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 );
		}
	}
}

                   


                   

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software