> php教程 > php手册 > 자신만의 템플릿 엔진 개발

자신만의 템플릿 엔진 개발

WBOY
풀어 주다: 2016-08-27 08:53:49
원래의
1041명이 탐색했습니다.

맞춤 템플릿 엔진 클래스

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');
로그인 후 복사

사용자 정의 보기

템플릿/show.html

<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>
로그인 후 복사

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿