PHP Development Essentials PHP template engine Dwoo (1) _php tutorial

Source: Internet
Author: User
Tags pear php template vars
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:

 
 
  1. < HTML >
  2. < Head > head >
  3. < Body >
  4. < blockquote >
  5. Knock knock! < BR />
  6. Who ' s there? < BR />
  7. {$name} < BR />
  8. {$name} who? < BR />
  9. {$punchline}
  10. blockquote >
  11. Body >
  12. 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:

 
 
  1. include ' dwooautoload.php ';
  2. //Create Dwoo instances
  3. $dwoo = New Dwoo ();
  4. //Read template file
  5. $TPL = New dwoo_template_file (' tmpl/knock.tpl ');
  6. //Assigning values to template variables
  7. $data = Array ();
  8. $data [' name '] = ' Boo ' ;
  9. $data [' punchline '] = ' Don ' t cry, it's only a joke ' ;
  10. //Export the actual content to the template
  11. $dwoo ->output ($tpl, $data);
  12. ?>

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. ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.