Essential for PHP development: Learning PHP template engine Dwoo step by step (1)

Source: Internet
Author: User
Tags php template

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:

 
 
  1. <body> 
  2. <blockquote> 
  3. Knock knock! <br/> 
  4. Who's there? <br/> 
  5. {$name} <br/> 
  6. {$name} who? <br/> 
  7. {$punchline}  
  8. </blockquote> 
  9. </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:

 
 
  1. <? Php
  2. Include 'dwooautoload. php ';
  3. // Create a dwoo instance
  4. $ Dwoo = new Dwoo ();
  5. // Read the Template File
  6. $ Tpl = new Dwoo_Template_File ('tmpl/knock. tpl ');
  7. // Assign values to template Variables
  8. $ Data = array ();
  9. $ Data ['name'] = 'boo ';
  10. $ Data ['punchline'] = 'don' t cry, it's only a joke ';
  11. // Output the actual content to the template
  12. $ Dwoo-> output ($ tpl, $ data );
  13. ?>

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:


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.