Home > php教程 > php手册 > Develop your own template engine

Develop your own template engine

WBOY
Release: 2016-08-27 08:53:49
Original
1051 people have browsed it

自定义模板引擎类

MyTpl.class.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php

</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MyTpl

</span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{

</span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$tpl_vars</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

</span><span style="color: #008080;"> 5</span>     <span style="color: #008000;">//</span><span style="color: #008000;">分配</span>

<span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> assign(<span style="color: #800080;">$key</span>,<span style="color: #800080;">$value</span><span style="color: #000000;">){

</span><span style="color: #008080;"> 7</span>         <span style="color: #800080;">$this</span>->tpl_vars[<span style="color: #800080;">$key</span>] = <span style="color: #800080;">$value</span><span style="color: #000000;">;

</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    }

</span><span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> display(<span style="color: #800080;">$tpl</span><span style="color: #000000;">){

</span><span style="color: #008080;">10</span>         <span style="color: #800080;">$contents</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$tpl</span><span style="color: #000000;">);

</span><span style="color: #008080;">11</span>         <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->tpl_vars <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">){

</span><span style="color: #008080;">12</span>         <span style="color: #008000;">//</span><span style="color: #008000;">替换 将{$name} 替换成真实的数据</span>

<span style="color: #008080;">13</span>         <span style="color: #800080;">$contents</span> = <span style="color: #008080;">str_replace</span>('{$'."<span style="color: #800080;">$k</span>".'}',"<span style="color: #800080;">$v</span>", <span style="color: #800080;">$contents</span><span style="color: #000000;">);

</span><span style="color: #008080;">14</span>         <span style="color: #800080;">$compile</span> = './templates_c/'.<span style="color: #008080;">md5</span>('show.html') . '.php'<span style="color: #000000;">;

</span><span style="color: #008080;">15</span>         <span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$compile</span>, <span style="color: #800080;">$contents</span><span style="color: #000000;">);

</span><span style="color: #008080;">16</span>         <span style="color: #0000ff;">require</span> <span style="color: #800080;">$compile</span><span style="color: #000000;">;

</span><span style="color: #008080;">17</span> <span style="color: #000000;">        }

</span><span style="color: #008080;">18</span> <span style="color: #000000;">    }

</span><span style="color: #008080;">19</span> <span style="color: #000000;">}

</span><span style="color: #008080;">20</span> <span style="color: #800080;">$tpl</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyTpl;

</span><span style="color: #008080;">21</span> <span style="color: #800080;">$tpl</span>-> assign('name','张四'<span style="color: #000000;">);

</span><span style="color: #008080;">22</span> <span style="color: #800080;">$tpl</span>-> display('./template/show.html');

Copy after login

 

自定义视图

template/show.html

1

2

3

4

5

6

7

8

9

10

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><!</span><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 3</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 4</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 5</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Insert title here<span style="color: #0000ff;"></</span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 6</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 7</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>

<span style="color: #008080;"> 8</span> <span style="color: #000000;">    {$name}

</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>

<span style="color: #008080;">10</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>

Copy after login

 

<span class="nt"> </span>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template