Home  >  Article  >  Backend Development  >  Necessary for PHP development. Learn PHP template engine Dwoo(1) step by step_PHP tutorial

Necessary for PHP development. Learn PHP template engine Dwoo(1) step by step_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:56:47944browse

PHP is currently one of the most widely used dynamic languages ​​for script parsing. In the development of PHP, one issue that developers are very concerned about is how to separate the page and business logic to the greatest extent. Many current PHP development frameworks have good solutions in this regard, such as Zend, Agavi, CakePHP and CodeIgniter. However, if your project is not too large and does not use these frameworks, you can choose some open source PHP template engines to separate pages and logic. Currently, the more famous one is Smarty. This article will introduce another emerging PHP template engine Dwoo, which also has many advantages and is worth learning by readers.

1. Install Dwoo

First go to Dwoo’s official website to download the latest version 1.1.7 (http://www.dwoo.org). After downloading, unzip dwoo and name the unzipped directory dwoo. Of course, you can also install it using the pear installation method:

pear channel-discover pearhub.org

pear install pearhub/Dwoo

2. Introduction to Dwoo templates

In Dwoo, similar to template engines such as Smarty, it allows users to edit with ordinary HTML editing tools. The page in the presentation layer is then represented by placeholders where dynamic content needs to be generated. When the template engine is parsing, it will fill these placeholders with results from the database or amateur logic calculations. Let's look at a simple example first.

Let’s create a template file first. Dwoo’s template file defaults to tpl. Of course, you can also change it to other file suffixes. The template file name is knock.tpl, save it in the template folder, the content is:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
<li>
<span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"><</span><span class="tag-name">blockquote</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span>Knock knock! </span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span>
</li>
<li>
<span>Who's there? </span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span>
</li>
<li class="alt">
<span>{$name} </span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span>
</li>
<li>
<span>{$name} who? </span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span> </span>
</li>
<li class="alt"><span>{$punchline}  </span></li>
<li>
<span class="tag"></</span><span class="tag-name">blockquote</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
</ol>

You can see that in Dwoo, in the template file, put the required dynamic The replaced content is wrapped in the form {$} as a placeholder, and the content in the placeholder will be automatically replaced with the actual content. Next, let’s look at how to use Dwoo. The code is as follows:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">include</span><span> </span><span class="string">'dwooAutoload.php'</span><span>;  </span></li><li class="alt"><span class="comment">// 创建dwoo实例 </span><span> </span></li><li><span class="vars">$dwoo</span><span> = </span><span class="keyword">new</span><span> Dwoo();  </span></li><li class="alt"><span class="comment">//读取模版文件  </span><span> </span></li><li><span class="vars">$tpl</span><span> = </span><span class="keyword">new</span><span> Dwoo_Template_File(</span><span class="string">'tmpl/knock.tpl'</span><span>);  </span></li><li class="alt"><span class="comment">// 对模版变量赋值 </span><span> </span></li><li><span class="vars">$data</span><span> = </span><span class="keyword">array</span><span>();  </span></li><li class="alt"><span class="vars">$data</span><span>[</span><span class="string">'name'</span><span>] = </span><span class="string">'Boo'</span><span>;  </span></li><li><span class="vars">$data</span><span>[</span><span class="string">'punchline'</span><span>] = </span><span class="string">'Don't cry, it's only a joke'</span><span>;  </span></li><li class="alt"><span class="comment">// 将实际内容输出到模版 </span><span> </span></li><li><span class="vars">$dwoo</span><span>->output(</span><span class="vars">$tpl</span><span>, </span><span class="vars">$data</span><span>);  </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>

The following are several steps to use Dwoo:

1. First of all Contains the Dwoo automatic loading class dwooAutoload.php, which automatically loads other dependent classes and tool classes required by the Dwoo template;

2. Create an instance of the Dwoo class;

3. Load the template through the new Dwoo_Template_File method, where the parameter is the path where the template file is located;

4. Set the replacement content to be output to the template file. In Dwoo, you only need to define an associative array method That’s it. The name of each element in the array corresponds to the placeholder in the template file. Each value in the array is to replace the actual content in the template;

5. By calling the output method , output the data to the template, and the parameters passed in are the output array content and template path.

The following picture shows the output result:


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445812.htmlTechArticlePHP is currently one of the most widely used dynamic languages ​​for script parsing. In the development of PHP, one issue that developers are very concerned about is how to separate the page and business logic to the greatest extent. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn