Template Technology in PHP

Source: Internet
Author: User
Tags arrays include php script
Review:

In many people to develop a large PHP project, template technology is very useful, it can separate the art and programmer work, and facilitate the modification of the interface and improve; not only that, using template technology, we can also simple and effective customization or modify the site. Now we're going to use the Phplib template as an example to illustrate how to apply template technology in PHP.

How do I use the phplib template?

We have a template named Usertemp, the path is/home/user_dir/user_temp/, and its contents are as follows:

Your order is: {Product}

The curly brace indicates that product is a template variable.

Then we write the following program:

Include "Template.inc";
$user _product = "Walkman";

$tmp = new Template ("/home/user_dir/user_temp/"); Create a template object named $t
$tmp->set_file ("FileHandle", "usertemp.ihtml"); Set handle filehandle = Template file
$tmp->set_var ("Product", $user _product); Set the template variable product= $user _product
$tmp->parse ("Output", "FileHandle"); Set the template variable Output = parsed file
$tmp->p ("Output"); Outputs output values (after our analysis of the data)
? >

template.inc is a file in Phplib, we use include to use the Phplib template feature. The Phplib template uses object-oriented design, so we can create a template object with $tmp = new Template ("/home/user_dir/user_temp/"), whose argument is a path ("/home/user_dir/user _temp/"), to set the location of the template file, the default path is the directory where the PHP script resides.

set_file () is used to define a handle "filehandle" that points to the usertemp.ihtml (phplib template file name suffix of. ihtml), and Set_var () is used to set the template variable product to $user_ The value of Product (that is, "Walkman"), the Parse () method loads the FileHandle (that is, usertemp.ihtml) to parse, replacing all occurrences of "{Product}" in the template with the $user_product value ("Walkman").

How do I use a nested template?

In the above example, the "Output" set by the parse () method is a template variable, and with this we can implement the nesting of templates.

For example, we have another template (assumed to be USERTEMP2), and its contents are:

Welcome, dear friend! {Output}

Then after analysis, its output will be:

Welcome, dear friend! Your order is: Walkman.

The following is an updated program:

Include "Template.inc";
$user _product = "Walkman";
$tmp = new Template ("/home/user_dir/user_temp/");
$tmp->set_file ("FileHandle", "usertemp.ihtml");
$tmp->set_var ("Product", $user _product);
$tmp->parse ("Output", "FileHandle");

$tmp->set_file ("FileHandle2", "usertemp2.ihtml");/set the second template handle
$tmp->parse ("Output", "FileHandle2");//analyze the second template
$tmp->p ("Output");
? >

It's very simple, we don't explain it in detail. Here's a tip: Parse () and P () can be written as a function pparse (), such as $tmp->pparse (Output "," FileHandle2).

How does a phplib template accept multiple sets of values?

The arguments for setfile () and Set_var () can be associative arrays (the handle is an array index, and the template file is a value) so that the template can accept multiple values, such as:

......
$tmp->setfile (Array ("FileHandle" => "usertemp.ihtml", "FileHandle2" => "usertemp2.ihtml"));
$tmp->set_var (Array ("Product" => "Walkman", "Product2" => "TV"));
......
? >

How do I append data to a template variable?

We can provide parse () and Pparse () with a third parameter (boolean variable) to append data to the template variable:

......
$tmp->pparse ("Output", "FileHandle", true);
......
? >

Thus, FileHandle is parsed and appended to the value of the output variable instead of a simple replacement.

Why use the block mechanism?

Let's say we want to show:

Your order is: Walkman TV, ...

With the above method directly appended, it may be shown that:

Your order is: Walkman you order is: TV you order is: ...

Obviously does not meet our requirements, then how to effectively solve this problem? Here you will use the block mechanism.

We will modify the above template file usertemp.ihtml:

Your order is:


{product}


This allows us to define a block named "Product_list".

The corresponding program is:

Include "Template.inc";
$tmp =new Template ("/home/user_dir/user_temp/");
$tmp->set_file ("FileHandle", "usertemp.ihtml");
$tmp->set_block ("FileHandle", "Product_list", "product_lists");
Replace the block in the file with {product_lists}

$tmp->set_var ("Product", "Walkman");
$tmp->parse ("Product_lists", "Product_list", true);
$tmp->set_var ("Product", "TV");
$tmp->parse ("Product_lists", "Product_list", true);
In specific use, you can use arrays and loops to do

$tmp->parse ("Output", "FileHandle");
$tmp->p ("Output");
? >

Now the output is the result we want.



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.