PHPIt is currently one of the most widely used scripts to parse dynamic languages. One of the issues that developers are concerned about in the development of PHP is how to maximize the separation of pages from business logic. Many of the current PHP development frameworks have good solutions in this area, such as zend,agavi,cakephp and CodeIgniter. However, if your project is not too large to use these frameworks, you can choose some open source PHP template engine to achieve the separation of the page and logic, is now more famous smarty. This article will introduce another new version of the PHP template engine
Dwoo, it also has a lot of advantages, worthy of readers to learn.
First, install the Dwoo
First go to Dwoo's official website to download (http://www.dwoo.org) The latest version of 1.1.7. After the download, unzip the Dwoo, the extract directory named Dwoo, of course, you can also use the Pear installation method to install, the method is:
Pear Channel-discover pearhub.org
Pear Install Pearhub/dwoo
Ii. Introduction of Dwoo Template
In Dwoo, similar to a template engine like smarty, it allows users to edit the presentation page with normal HTML editing tools, and then use placeholders in places where dynamic content needs to be generated. The template engine, when parsing, populates these placeholders with results such as in-database or amateur logic calculations. Let's look at a simple example below.
We first set up a template file, Dwoo template file is the default TPL, of course, you can also change to another file suffix. The template file is named Knock.tpl and is saved in the template folder with the following contents:
- < HTML >
- < Head > head >
- < Body >
- < blockquote >
- Knock knock! < BR />
- Who ' s there? < BR />
- {$name} < BR />
- {$name} who? < BR />
- {$punchline}
- blockquote >
- Body >
- HTML >
As you can see, in Dwoo, in the template file, the content that needs to be dynamically replaced is wrapped up in the form of {$}, and as a placeholder, the contents of the placeholder are automatically replaced with the actual content. Next look at how to use Dwoo, the code is as follows:
-
- include ' dwooautoload.php ';
- //Create Dwoo instances
- $dwoo = New Dwoo ();
- //Read template file
- $TPL = New dwoo_template_file (' tmpl/knock.tpl ');
- //Assigning values to template variables
- $data = Array ();
- $data [' name '] = ' Boo ' ;
- $data [' punchline '] = ' Don ' t cry, it's only a joke ' ;
- //Export the actual content to the template
- $dwoo ->output ($tpl, $data);
- ?>
Here are a few steps to using Dwoo:
1, the first to include Dwoo automatic loading class dwooautoload.php, this class is automatically loaded Dwoo template needs other dependent classes and tool classes;
2, create an instance of the Dwoo class;
3, through the new Dwoo_template_file method to load the template, wherein the parameters of the template file is located in the path;
4, set to the template file to output the replacement, in Dwoo, only need to define an associative array method, the array of each element name and template file in the placeholder one by one corresponding to each value in the array is to replace the actual content in the template;
5, by invoking the output method, the data is exported to the template, the passed parameter is the output array content and the template path.
To output the result:
http://www.bkjia.com/PHPjc/445812.html www.bkjia.com true http://www.bkjia.com/PHPjc/445812.html techarticle PHP is currently one of the most widely used script parsing dynamic languages. One of the issues that developers are concerned about in the development of PHP is how to maximize the separation of pages from business logic. ...