> php教程 > PHP源码 > 본문

php超实用的模板引擎

PHP中文网
풀어 주다: 2016-05-25 17:13:56
원래의
1141명이 탐색했습니다.

方法: 

$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 );
		}
	}
}
로그인 후 복사

                   


                   

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!