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

开发自己的模板引擎

WBOY
发布: 2016-08-27 08:53:49
原创
1037 人浏览过

自定义模板引擎类

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');
登录后复制

 

自定义视图

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>
登录后复制

 

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

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