首页 > php教程 > PHP源码 > 编译型PHP模板引擎大致实现过程

编译型PHP模板引擎大致实现过程

PHP中文网
发布: 2016-05-25 17:10:49
原创
1414 人浏览过

模板引擎

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

<?php

/**

 * @author Jiawei

 * @Completed in 2012-6-29 0:23

 */

class JTemplate{

    //通过assign函数传入的变量临时存放数组

    private $templateVar = array();

    //模板目录

    private $templateDir = &#39;&#39;;

    //编译目录

    private $templateCompileDir = &#39;&#39;;

      

    private $fileName = &#39;&#39;;

    /**

     * 构造函数

     * @param string $templateDir 模板目录

     * @param string $templateComplieDir 模板编译目录

     */

    public function __construct($templateDir,$templateComplieDir){

        $this->templateDir = $templateDir;

        $this->templateCompileDir = $templateComplieDir;

    }

    /**

     * 显示模板

     * @param string $fileName 模板文件名

     */

    public function display($fileName){

        $this->fileName = $fileName;

        if(file_exists($this->templateDir.&#39;/&#39;.$this->fileName)){

            $compileFileName = $this->templateCompileDir.&#39;/&#39;.$this->file_safe_name().&#39;.php&#39;;

            if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.&#39;/&#39;.$this->fileName)){

                $this->del_old_file();

                $this->compile();

            }

            extract($this->templateVar);

            include $compileFileName;

        }else{

            $this->error(&#39;Sorry,the template file &#39;.$this->fileName.&#39; does not exist!!&#39;);

        }

    }

    /**

     * 获取编译文件名

     */

    private function get_compile_file(){

        $compileFile = explode(&#39;.&#39;,$this->fileName);

        unset($compileFile[count($compileFile)-1]);

        return implode(&#39;.&#39;,$compileFile);

    }

    /**

     * 编译

     */

    private function compile(){

        $fileHandle = @fopen($this->templateDir.&#39;/&#39;.$this->fileName, &#39;r&#39;);

        while(!feof($fileHandle)){

            $fileContent = fread($fileHandle,1024);

        }

        fclose($fileHandle);

        $fileContent = $this->template_replace($fileContent);

        //$compileFile = $this->get_compile_file($fileName);

        $fileHandle = @fopen($this->templateCompileDir.&#39;/&#39;.$this->file_safe_name().&#39;.php&#39;,&#39;w&#39;);

        if($fileHandle){

            fwrite($fileHandle, $fileContent);

            fclose($fileHandle);

        }else{

            $this->error(&#39;Sorry,Compile dir can not write!&#39;);

        }

    }

    /**

     * 模板传值

     * @param string $valueName 模板中使用的变量名

     * @param $value 变量值

     */

    public function assign($valueName,$value){

        $this->templateVar[$valueName] = $value;

    }

      

    /**

     * 模板正则替换

     * @param string $content 替换内容

     * @return string 替换过后的内容

     */

    private function template_replace($content){

        $orginArray = array(

            &#39;/<!--loop\s+\$(\w+)\s+\$(\w+)-->/s&#39;,

            &#39;/<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s&#39;,

            &#39;/<!--elseloop-->(.+?)<!--endloop-->/s&#39;,

            &#39;/<!--endloop-->/s&#39;,

            &#39;/<!--if\s+\((.+?)\)-->/s&#39;,

            &#39;/<!--endif-->/s&#39;,

            &#39;/<!--elseif\s+\((.+?)\)-->/s&#39;,

            &#39;/<!--else-->/s&#39;,

            &#39;/\{P:(.+?)\:}/s&#39;,

            &#39;/\{C:(\w+)\}/s&#39;,

            &#39;/\{I:(.+?)\}/s&#39;,

            &#39;/\{F:(.+?)\}/s&#39;,

            &#39;/\{EF:(.+?)\}/s&#39;,

            &#39;/\{([a-zA-Z0-9_\[\]\&#39;\"\$\.\x7f-\xff]+)\}/s&#39;,

        );

          

        $changeArray = array(

            &#39;<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?>&#39;,

            &#39;<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=>$$3){$countLoop++;?>&#39;,

            &#39;<?php }if(!empty($countLoop))$countLoop--;}else{?>$1<?php }?>&#39;,

            &#39;<?php }if(!empty($countLoop))$countLoop--;}?>&#39;,

            &#39;<?php if($1){?>&#39;,

            &#39;<?php }?>&#39;,

            &#39;<?php }elseif($1){?>&#39;,

            &#39;<?php }else{?>&#39;,

            &#39;<?php $1?>&#39;,

            &#39;<?php echo $1;?>&#39;,

            &#39;<?php include_once "&#39;.$this->templateDir.&#39;/$1";?>&#39;,

            &#39;<?php $1;?>&#39;,

            &#39;<?php echo $1;?>&#39;,

            &#39;<?php echo $$1;?>&#39;,

        );

        return $repContent=preg_replace($orginArray,$changeArray,$content);

    }

    /**

     * 删除旧文件

     */

    private function del_old_file(){

        $compileFile = $this->get_compile_file($this->fileName);

        $files = glob($this->templateCompileDir.&#39;/&#39;.$compileFile.&#39;*.php&#39;);

        // print_r($files);

        if($files){

            @unlink($files[0]);

        }

    }

    /**

     * 编译文件名安全处理方法

     * @return string 返回编译文件名

     */

    private function file_safe_name(){

        $compileFile = $this->get_compile_file($this->fileName);

        return $compileFile.filemtime($this->templateDir.&#39;/&#39;.$this->fileName);

    }

      

    /**

     * 错误输出函数

     * @param string $content 错误输出信息

     */

    private function error($content){

        $stringHtml = &#39;<div style="width:780px;height:auto;padding:10px;border:1px solid #CCC;margin:0 auto;">&#39;;

        $stringHtml .= &#39;Error information:<br />&#39;;

        $stringHtml .= &#39;<font color="red">&#39;;

        $stringHtml .= $content;

        $stringHtml .= &#39;</font>&#39;;

        $stringHtml .= &#39;</div>&#39;;

        exit($stringHtml);

    }

}

?>

登录后复制

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板