一个自己写的PHP模板引擎

来源:互联网 发布:医疗器械临床数据库 编辑:程序博客网 时间:2024/05/02 01:06

这是一个自己写的编译型的模板引擎(不包括缓存部分)贴上代码。有问题可以随时跟帖。

单文件版:JTemplate.class.php

 

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
<!--?php
/**
 * @author Jiawei
 * @copyright Jiawei
 * @Completed in 2012-6-29 0:23
 * JTemplate V1.0Beta
 */
classJTemplate{
    //通过assign函数传入的变量临时存放数组
    private$templateVar = array();
    //模板目录
    private$templateDir = '';
    //编译目录
    private$templateCompileDir = '';
    /**
     * 构造函数
     * @param string $templateDir 模板目录
     * @param string $templateComplieDir 模板编译目录
     */
    publicfunction __construct($templateDir,$templateComplieDir){
        $this--->templateDir = $templateDir;
        $this->templateCompileDir = $templateComplieDir;
    }
    /**
     * 显示模板方法
     * @param string $fileName 模板文件名
     */
    publicfunction display($fileName){
        if(file_exists($this->templateDir.'/'.$fileName)){
            $compileFileName= $this->templateCompileDir.'/'.$this->file_safe_name($fileName).'.php';
            if(!file_exists($compileFileName) || filemtime($compileFileName)<filemtime($this->templateDir.'/'.$fileName)){
                $this->del_old_file($fileName);
                $this->compile($fileName);
            }
            extract($this->templateVar);
            include$compileFileName;
        }else{
            $this->error('Sorry,the template file '.$fileName.' does not exist!!');
        }
    }
    /**
     * 获取编译文件名方法
     * @param string $fileName 模板文件名
     */
    publicfunction get_compile_file($fileName){
        $compileFile= explode('.',$fileName);
        unset($compileFile[count($compileFile)-1]);
        returnimplode('.',$compileFile);
    }
    /**
     * 编译方法
     * @param string $fileName 模板文件名
     */
    publicfunction compile($fileName){
        $fileHandle= @fopen($this->templateDir.'/'.$fileName,'r');
        while(!feof($fileHandle)){
            $fileContent= fread($fileHandle,1024);
        }
        fclose($fileHandle);
        $fileContent= $this->template_replace($fileContent);
        //$compileFile = $this->get_compile_file($fileName);
        $fileHandle= @fopen($this->templateCompileDir.'/'.$this->file_safe_name($fileName).'.php','w');
        if($fileHandle){
            fwrite($fileHandle,$fileContent);
            fclose($fileHandle);
        }else{
            $this->error('Sorry,Compile dir can not write!');
        }
    }
    /**
     * 模板传值方法
     * @param string $valueName 模板中使用的变量名
     * @param all $value 变量值
     */
    publicfunction assign($valueName,$value){
        $this->templateVar[$valueName] = $value;
    }
        
    /**
     * 模板正则替换方法
     * @param string $content 替换内容
     * @return string 替换过后的内容
     */
    publicfunction template_replace($content){
        $orginArray= array(
            '/{C}<!--loop\s+\$(\w+)\s+\$(\w+)-->/s',
            '/{C}<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s',
            '/{C}<!--elseloop-->(.+?){C}<!--endloop-->/s',
            '/{C}<!--endloop-->/s',
            '/{C}<!--if\s+\((.+?)\)-->/s',
            '/{C}<!--endif-->/s',
            '/{C}<!--elseif\s+\((.+?)\)-->/s',
            '/{C}<!--else-->/s',
            '/\{P:(.+?)\:}/s',
            '/\{C:(\w+)\}/s',
            '/\{I:(.+?)\}/s',
            '/\{F:(.+?)\}/s',
            '/\{EF:(.+?)\}/s',
            '/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s',
        );
            
        $changeArray= array(
            '<!--?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?-->',
            '<!--?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=-->$$3){$countLoop++;?>',
            '<!--?php }if(!empty($countLoop))$countLoop--;}else{?-->$1<!--?php }?-->',
            '<!--?php }if(!empty($countLoop))$countLoop--;}?-->',
            '<!--?php if($1){?-->',
            '<!--?php }?-->',
            '<!--?php }elseif($1){?-->',
            '<!--?php }else{?-->',
            '<!--?php $1?-->',
            '<!--?php echo $1;?-->',
            '<!--?php include_once "'.$this--->templateDir.'/$1";?>',
            '<!--?php $1;?-->',
            '<!--?php echo $1;?-->',
            '<!--?php echo $$1;?-->',
        );
        return$repContent=preg_replace($orginArray,$changeArray,$content);
    }
    /**
     * 删除旧文件
     * @param string $fileName 模板文件名
     */
    publicfunction del_old_file($fileName){
        $compileFile= $this->get_compile_file($fileName);
        $files= glob($this->templateCompileDir.'/'.$compileFile.'*.php');
        // print_r($files);
        if($files){
            @unlink($files[0]);
        }
    }
    /**
     * 编译文件名安全处理方法
     * @param string $fileName 传入模板文件名
     * @param string 返回编译文件名
     */
    publicfunction file_safe_name($fileName){
        $compileFile= $this->get_compile_file($fileName);
        return$compileFile.filemtime($this->templateDir.'/'.$fileName);
    }
        
    /**
     * 错误输出函数
     * @param string $content 错误输出信息
     */
    publicfunction error($content){
        $stringHtml= '
'; $stringHtml .= 'Error information:
'; $stringHtml .= ''; $stringHtml .= $content; $stringHtml .= ''; $stringHtml .= ' '; exit($stringHtml); } } ?>

 

模板引擎使用方法:

首先载入模板引擎核心文件JTemplate.class.php

include_once 'JTemplate/JTemplate.class.php';

实例化模板引擎:

$template = new JTemplate(模板目录,编译目录);

模板引擎方法:

1.assign方法用来将值传入模板中

$template->assign('模板引擎中用的变量名');

2.display显示模板文件方法:

$template->display('模板文件');

模板语法使用方法:

输出变量

{变量名} //不带$符号

判断语句

内容1内容2内容3//可拆分使用

遍历数组

循环内容

相当于

foreach($a as $v){}

遍历数组2:

循环内容当数组为空或传入变量不为数组的时候输出的内容

遍历中的计数器

要在遍历中使用计数器可以使用变量{countLoop}来计算当前循环的次数如果在if中使用请使用$countLoop

{countLoop} //在循环内输出当前循环的次数。在循环外使用输出最近的一次循环循环了多少次

//if语句中这样使用

输出常量

{C:常量名}

载入文件

{I:载入文件的路径及文件名及后缀}

执行函数不输出

{F:函数名(参数,参数)}

执行函数并输出函数返回的结果

{EF:函数名(参数,参数)}

执行PHP原生语句

{P:PHP语句:}

0 0
原创粉丝点击