<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>落雪实验室 &#187; discuz!</title>
	<atom:link href="http://www.fallensnow.net/index.php/archives/tag/discuz/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fallensnow.net</link>
	<description>收藏分享精品软件与技术-关注互联网、操作系统与搜索引擎</description>
	<lastBuildDate>Sat, 20 Nov 2010 16:33:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>php额外学习－MVC－从PHP的模板引擎看Discuz!模板机制</title>
		<link>http://www.fallensnow.net/index.php/archives/94</link>
		<comments>http://www.fallensnow.net/index.php/archives/94#comments</comments>
		<pubDate>Wed, 08 Apr 2009 01:16:37 +0000</pubDate>
		<dc:creator>落雪</dc:creator>
				<category><![CDATA[网站开发设计]]></category>
		<category><![CDATA[discuz!]]></category>
		<category><![CDATA[phplib]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.fallensnow.com.cn/?p=94</guid>
		<description><![CDATA[<p><strong>前言</strong></p><p>一、<strong>关于模板引擎的前言</strong><br />从phplib到smarty，再到Discuz!的模板机制，本文试图通过PHP模板引擎为你讲解作者自己的PHP心得。</p><p>我清楚的记得在我刚上大学开始学习PHP的时候，曾经在phpe.net看到过一篇关于phplib Template和FastTemplate这两模板引擎性能比较的文章。让我在接下来半年的时间内持续的使用着phplib。不可否认phplib是左 右了一代PHP开发人员对于PHP模板引擎的认识。或许你也会对下面的方法比较熟悉</p>...]]></description>
			<content:encoded><![CDATA[<p><strong>前言</strong></p>
<p>一、<strong>关于模板引擎的前言</strong><br />
从phplib到smarty，再到Discuz!的模板机制，本文试图通过PHP模板引擎为你讲解作者自己的PHP心得。</p>
<p>我清楚的记得在我刚上大学开始学习PHP的时候，曾经在phpe.net看到过一篇关于phplib Template和FastTemplate这两模板引擎性能比较的文章。让我在接下来半年的时间内持续的使用着phplib。不可否认phplib是左 右了一代PHP开发人员对于PHP模板引擎的认识。或许你也会对下面的方法比较熟悉</p>
<div class="hl-surround">
<div class="hl-main">$t-&gt;set_file<br />
$t-&gt;set_var</div>
</div>
<p>当我对于phplib的执行效率不满意的时候，我开始寻找下一个PHP的模板引擎，于是smarty跳入我的视野范围，当我费尽心血去学会了smarty并使用开发了很多东西，而现在的我突然发现记得的也就只有下面的方法了</p>
<div class="hl-surround">
<div class="hl-main">$s-&gt;assign<br />
$s-&gt;display</div>
</div>
<p>究竟我们需要模板引擎来做什么呢，MVC？简单？易用？效率？请看下文的分析。</p>
<p><span id="more-94"></span>二、<strong>程序处理的分析</strong></p>
<p>1.<strong>PHPLIB的程序处理过程</strong></p>
<p>从phplib的处理开始讲起</p>
<div class="hl-surround">
<div class="hl-main">$t = new Template()<br />
$t-&gt;set_file<br />
$t-&gt;set_var<br />
$t-&gt;parse<br />
$t-&gt;p</div>
</div>
<p>看上面的代码，翻译成中文就是</p>
<ul>
<li>初始化模板类$t</li>
<li>设置模板文件</li>
<li>设置模板变量</li>
<li>分析模板文件中的模板变量</li>
<li>输出内容</li>
</ul>
<p>通过了最少5个步骤在php程序中实现模板的处理</p>
<p>2.<strong>Smarty的程序处理过程</strong></p>
<p>现在来看smarty的处理</p>
<div class="hl-surround">
<div class="hl-main">$s = new Smarty<br />
$s-&gt;assign<br />
$s-&gt;display</div>
</div>
<p>翻译成中文就是</p>
<ul>
<li>初始化模板类$s</li>
<li>设置模板变量</li>
<li>解析并输出模板</li>
</ul>
<p>3.<strong>Discuz!模板的程序处理过程</strong></p>
<div class="hl-surround">
<div class="hl-main">include template(tplname);</div>
</div>
<p>主要作用就是指定给程序需要处理的模板文件</p>
<p>在上述三种模板处理机制中，最容易理解和接受就是Discuz!模板的处理过程。初始化、设置变量、解析模板、输出内容，Discuz!只用了一个 函数来做。对于一个开源的论坛软件，这样处理的好处是显而易见的，对于Discuz!进行二次开发的程序员的要求降低。简化模板语言，方便风格和插件的制 作，这也在一定程度上促进了Discuz!的传播</p>
<p>三、<strong>模板源文件的语法</strong></p>
<p>在phplib中处理循环嵌套的时候，使用：</p>
<div class="hl-surround">
<div class="hl-main">&lt;!&#8211;    BEGIN row   &#8211;&gt;<br />
{it}<br />
&lt;!&#8211;     END row    &#8211;&gt;</div>
</div>
<p>在smarty中处理循环嵌套的时候，引入了</p>
<div class="hl-surround">
<div class="hl-main">&lt; {section name=loopName loop=$loopArray}&gt;（当然还有foreach这样的）</div>
</div>
<p>在Discuz!中处理循环嵌套的时候，</p>
<div class="hl-surround">
<div class="hl-main">&lt;!&#8211;{loop $array $value}&#8211;&gt;</div>
</div>
<p>其实真正的模板面对的可以说是不懂PHP或者懂一点PHP的美工同志们，模板的复杂就意味着美工制作页面的难度加大。在必不可少的需要模板有逻辑处 理的时候，为什么不在html代码中使用原生态的PHP语法，而让美工相当于去学习另外一种语言呢？在我个人的经验中，显然是Discuz!的模板语言更 为简单易学，也为我节省了更多的时间。</p>
<p>四、<strong>Discuz!模板处理机制</strong><br />
我剥离出一个简单的Discuz!模板处理函数</p>
<div class="hl-surround">
<div class="hl-main">function template($file, $templateid = 0, $tpldir = &#8221;) {$tplfile = DISCUZ_ROOT.&#8217;./&#8217;.$tpldir.&#8217;/&#8217;.$file.&#8217;.htm&#8217;;//模板源文件，此处$tplfile变量的值可能是D:\discuz\templates\default\demo.htm<br />
$objfile = DISCUZ_ROOT.&#8217;./forumdata/templates/&#8217;.<br />
$templateid.&#8217;_&#8217;.$file.&#8217;.tpl.php&#8217;;//模板缓存文件，此处$objfile变量的值可能是D:\discuz\forumdata\templates\1_demo.tpl.php</p>
<p>//如果模板源文件的修改时间迟于模板缓存文件的修改时间，<br />
//就是模板源文件被修改而模板缓存没有更新的时候，<br />
//则调用parse_template函数重新生成模板缓存文件。<br />
if(@filemtime($tplfile) &gt; @filemtime($objfile)) {<br />
require_once DISCUZ_ROOT.&#8217;./include/template.func.php&#8217;;<br />
parse_template($file, $templateid, $tpldir);<br />
}</p>
<p>//返回缓存文件名称<br />
//$objfile变量内容可能为D:\discuz\forumdata\templates\1_demo.tpl.php<br />
return $objfile;<br />
}</p></div>
</div>
<p>而php页面的模板执行语句</p>
<div class="hl-surround">
<div class="hl-main">include template(&#8216;demo&#8217;);</div>
</div>
<p>实际上在本例中就是相当于</p>
<div class="hl-surround">
<div class="hl-main">include &#8216;D:\discuz\forumdata\templates\1_demo.tpl.php&#8217;;</div>
</div>
<p>这个流程就是一个demo.php文件中当数据处理完成以后include template(’demo’)，去显示页面。</p>
<p>五、<strong>总结</strong><br />
我也曾经看到过有列举出很多种的PHP模板引擎，但是我觉着phplib、smarty、Discuz!模板机制就足以说明问题了。</p>
<p>1.我们需要模板来做什么？<br />
分离程序与界面，为程序开发以及后期维护提供方便。</p>
<p>2.我们还在关心什么？<br />
PHP模板引擎的效率，易用性，可维护性。</p>
<h2  class="related_post_title">相关文章：</h2><ul class="related_post"><li>2009/06/24 -- <a href="http://www.fallensnow.net/index.php/archives/510" title="为 Windows 7添加智能ABC输入法">为 Windows 7添加智能ABC输入法</a></li><li>2009/06/14 -- <a href="http://www.fallensnow.net/index.php/archives/389" title="微软Office 2010 技术预览版(Technical Preview) 抢先试用">微软Office 2010 技术预览版(Technical Preview) 抢先试用</a></li><li>2010/06/19 -- <a href="http://www.fallensnow.net/index.php/archives/1585" title="互联网产品设计的40个超实用的Web设计工具">互联网产品设计的40个超实用的Web设计工具</a></li><li>2009/08/13 -- <a href="http://www.fallensnow.net/index.php/archives/771" title="微软LiveMesh,在Windows 7 RTM上体验基于云计算的微软文件同步系统">微软LiveMesh,在Windows 7 RTM上体验基于云计算的微软文件同步系统</a></li><li>2010/01/29 -- <a href="http://www.fallensnow.net/index.php/archives/1418" title="轻松转换PPT文件为SWF动画 &mdash;&mdash; iSpring Free！">轻松转换PPT文件为SWF动画 &mdash;&mdash; iSpring Free！</a></li><li>2009/08/08 -- <a href="http://www.fallensnow.net/index.php/archives/664" title="最新！ubuntu下优秀的优化软件 Tweak 0.4.8 推出 （含下载）">最新！ubuntu下优秀的优化软件 Tweak 0.4.8 推出 （含下载）</a></li></ul>
	标签：<a href="http://www.fallensnow.net/index.php/archives/tag/phplib" title="phplib" rel="tag nofollow">phplib</a>, <a href="http://www.fallensnow.net/index.php/archives/tag/discuz" title="discuz!" rel="tag nofollow">discuz!</a>, <a href="http://www.fallensnow.net/index.php/archives/category/web-design" title="网站开发设计" rel="tag nofollow">网站开发设计</a>, <a href="http://www.fallensnow.net/index.php/archives/tag/smarty" title="smarty" rel="tag nofollow">smarty</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.fallensnow.net/index.php/archives/94/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

