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>