Rumah > php教程 > php手册 > 开发自己的模板引擎

开发自己的模板引擎

WBOY
Lepaskan: 2016-08-27 08:53:49
asal
1053 orang telah melayarinya

自定义模板引擎类

MyTpl.class.php

<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');
Salin selepas log masuk

 

自定义视图

template/show.html

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 4</span> <span style="color: #0000ff;"><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 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 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></span></span></span></span></span></span>
Salin selepas log masuk

 

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan