牛骨文教育服务平台(让学习变的简单)

16.6.3 模板引擎讲解

什么是模版引擎?

百度百科的解释:
为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。
是不是听起来很晕?

用一句白话解释:
就是把静态页面中特定的一些标记(字符串)替换成最终需要的PHP标签;

好处:
可以让(网站)程序实现界面与数据分离,这就大大提升了开发效率,好的设计也使得代码重用变得更加容易。

编写自己的模板引擎

这里需要注意两点:
1 字符串替换,将{} 分别替换成 PHP 标签 <?php?>
2 为提升程序执行效率,使用模板缓存;

<?php

	function template($tFile)
    {
		$tmpFile = TPL_SKIN."/".$tFile;
		if(!file_exists($tmpFile)){
			return $tmpFile;
		}

		$comFile = TPL_CACHE."/".basename($tFile,".html").".php";
		if (!file_exists($comFile) || filemtime($tmpFile)>filemtime($comFile))
		{
			tplParse($tmpFile,$comFile);
		}

    		return $comFile;
	}


	function tplParse($tFile,$cFile){

		$fileContent=file_get_contents($tFile);

		$fileContent=preg_replace_callback("/^(xefxbbxbf)/", function($r){}, $fileContent);
		$fileContent=preg_replace_callback(
			"/<!--s*${(.+?)}s*-->/is",
			function($r)
			{
				return str_replace(""", """, "<?php echo " . $r[1] . "; ?>");
			},
			$fileContent
		);

		$fileContent=preg_replace_callback(
			"/{($[a-zA-Z0-9_[] -",\%*/.()>""$x7f-xff]+)}/s",
			function($r)
			{
				return str_replace(""", """, "<?php echo " . $r[1] . "; ?>");
			},
			$fileContent
		);
		$fileContent=preg_replace_callback(
			"/${(.+?)}/is", 
			function($r)
                        {
                                return str_replace(""", """, "<?php echo " . $r[1] . "; ?>");
                        },
			$fileContent
		);
		$fileContent=preg_replace_callback(
			"/<!--s*{elses*ifs+(.+?)}s*-->/is", 
			function($r)
                        {
                                return str_replace(""", """, "<?php } else if(" . $r[1] . "){ ?>");
                        },
			$fileContent
		);
		$fileContent=preg_replace_callback(
			"/<!--s*{elifs+(.+?)}s*-->/is", 
			function($r)
                        {
                                return str_replace(""", """, "<?php } else if(" . $r[1] . "){ ?>");
                        },
			$fileContent
		);
		$fileContent=preg_replace_callback(
			"/<!--s*{else}s*-->/is", 
			function($r)
                        {
                                return "<?php } else { ?>";
                        },
			$fileContent
		);
		for($i=0; $i<5; ++$i){
			$fileContent = preg_replace_callback(
				"/<!--s*{loops+(S+)s+(S+)s+(S+)s*}s*-->(.+?)<!--s*{/loop}s*-->/is", 
				function($r)
                        	{
                                	return str_replace(""", """, "<?php if(is_array(".$r[1].")){foreach(".$r[1]." AS ".$r[2]."=>".$r[3].") { ?>".$r[4]."<?php }}?>");
                        	},
				$fileContent
			);
			$fileContent = preg_replace_callback(
				"/<!--s*{loops+(S+)s+(S+)s*}s*-->(.+?)<!--s*{/loop}s*-->/is", 
				function($r)
                                {
                                        return str_replace(""", """, "<?php if(is_array(".$r[1].")){foreach(".$r[1]." AS ".$r[2].") { ?>".$r[3]."<?php }}?>");
                                },
				$fileContent
			);
			$fileContent = preg_replace_callback(
				"/<!--s*{ifs+(.+?)}s*-->(.+?)<!--s*{/if}s*-->/is", 
				function($r)
                                {
                                        return str_replace(""", """, "<?php if(".$r[1]."){?>".$r[2]."<?php }?>");
                                },
				$fileContent
			);
		}

		$fileContent = preg_replace_callback(
			"#<!--s*{s*includes+([^{}]+)s*}s*-->#i", 
			function($r)
            {
                return "<?php include template("".$r[1]."");?>";
            },
			$fileContent
		);

		file_put_contents($cFile,$fileContent);

		return true;

	}

如何使用自己的模板引擎?

公共文件/common/common.php中已经载入了模版引擎函数库,并指定了模板风格;
/config/config.php 中通过修改 define("TPL_SKIN", "theme/default"); 的值来,改变应用的模版风格(确保模板风格已经存在)

一 创建模板文件时使用的基本语法

1 echo 输出变量:

{$title}

2 if 判断:

{if $bigid}
...
{else}
...
{/if}

3 foreach 循环:

{loop $LTsMenu $key $val}
{$val["title"]}
{/loop}

4 模版中使用函数

${getUserName($val["compere"])}

5 include 模板中包含模板:

{include header.html}

二 使用模板

1 分配变量

在包含模版前定义的PHP变量,都可以在模版中使用:

$title = "首页 - " . WEB_NAME;

2 包含模版文件

首先查找是否已经缓存,若未缓存 则替换标签后生成缓存文件。

include template("index.html");