PHPCurrently, it is one of the most widely used Dynamic Language for parsing scripts. In PHP development, developers are very concerned about how to maximize the separation of pages and business logic. Currently, many PHP development frameworks have good solutions in this regard, such as Zend, Agavi, CakePHP, and CodeIgniter. However, if your project is not too large to use these frameworks, you can use some open-source PHP template engines to separate pages from logic. Currently, Smarty is a famous one. This article introduces another emerging PHP template engine.DwooIt also has many advantages and is worth learning.
1. Install Dwoo
First download the latest version 1.1.7 from the official website of Dwoo (http://www.dwoo.org. After downloading, decompress dwoo and name the decompressed directory dwoo. Of course, you can also install it by using the pear installation method:
Pear channel-discover pearhub.org
Pear install pearhub/Dwoo
II. Introduction to the Dwoo Template
In Dwoo, similar to template engines such as Smarty, it allows users to use common HTML editing tools to edit pages on the presentation layer, then, use a placeholder to represent the dynamic content. When parsing, the template engine will fill in the database or amateur logic computing results into these placeholders. The following is a simple example.
Create a template file. The default template file of Dwoo is tpl. You can also change it to another file suffix. The template file name is knock. tpl. Save it in the template folder with the following content:
-
-
- <body>
- <blockquote>
- Knock knock! <br/>
- Who's there? <br/>
- {$name} <br/>
- {$name} who? <br/>
- {$punchline}
- </blockquote>
- </body>
-
We can see that in the Dwoo template file, the content to be dynamically replaced is wrapped in the form of {$} as a placeholder, the content in the placeholder is automatically replaced with the actual content. The following code shows how to use Dwoo:
- <? Php
- Include 'dwooautoload. php ';
- // Create a dwoo instance
- $ Dwoo = new Dwoo ();
- // Read the Template File
- $ Tpl = new Dwoo_Template_File ('tmpl/knock. tpl ');
- // Assign values to template Variables
- $ Data = array ();
- $ Data ['name'] = 'boo ';
- $ Data ['punchline'] = 'don' t cry, it's only a joke ';
- // Output the actual content to the template
- $ Dwoo-> output ($ tpl, $ data );
- ?>
The following are the steps for using Dwoo:
1. The dwooAutoload. php class of the Dwoo automatic loading class should be included first. This class is the other dependent classes and tool classes required to automatically load the Dwoo template;
2. Create a Dwoo class instance;
3. Use the new Dwoo_Template_File method to load the template. The parameters are the path of the template file;
4. Set the replacement content to be output to the template file. In Dwoo, you only need to define an associated array method, the names of each element in the array correspond to placeholders in the template file one by one. Each value in the array is to replace the actual content in the template;
5. Call the output method to output data to the template. The input parameters are the output array content and template path.
Is the output result: