Phplib Template Introductory Series template nesting

Source: Internet
Author: User
Tags foreach config final variables php file require advantage

In a PHP program, we often write "public code" or "common parts" into a file, like our system configuration file, such as config.php, or public functions written in a functions.php file, which is like a page header that a site needs to use, The tail. The advantage of this is that it is convenient to maintain the site, and if the common part to change, no need to change every page, greatly reduce our workload. Phplib Template Starter Series-4 template nesting
Landlord
In a PHP program, we often write "public code" or "common parts" into a file, like our system configuration file, such as config.php, or public functions written in a functions.php file, which is like a page header that a site needs to use, The tail. The advantage of this is that it is convenient to maintain the site, and if the common part to change, no need to change every page, greatly reduce our workload.
Before you may use Require,include (require_once,include_once) to introduce a common page head, really convenient and effective, now we use Template template class can also be implemented, And you can easily insert a page into another template anywhere. If you want to also make a template that contains variables for the page you want to insert, you'll find that the template class handles the job very well.

Create a new three file third.html,header.html,footer.html in the template directory. The content is divided into the following
Third.html
<!--This is the page head-->

<BODY>
Here is a list
<UL>
<!--BEGIN list-->
<li> 's achievement is
<!--end list-->
</UL>
<!--This is the foot of the page-->

</BODY>
</HTML>

Header.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>

Footer.html
<p>author©iwind

As you can see, the examples in our previous sections are
$TPL->set_file ("main", "Template filename");
To load the template file. Only so named "main" is because we want to give it a meaning: the main template. The third.html can be called a "master template", and the template file header.html that you want to embed in the third.html of the main template is called a "child template". Also footer.html is a "child template", We want to place it in the main template. A master template can be arbitrarily embedded in any number of content, size, format, etc. of any multiple child templates.

Below we start our PHP program.

First, create an instance object of a class
Include in template class Template.inc
Require "inc/template.inc";

Create an instance
$TPL = new Template ("Template");

Read the contents of three template files, respectively, to the variable "main", "My_header", "My_footer"
$TPL->set_file ("main", "third.html");
$tpl->set_file ("My_header", "header.html");
$tpl->set_file ("My_footer", "footer.html");

Executes the template variable substitution in the My_header,my_footer and assigns the final result to the Header,footer in the main template, respectively.
$tpl->parse ("header", "My_header");
$tpl->parse ("Footer", "My_footer");

Then complete the replacement of the main template variables, and output the main template after the analysis of the content
$tpl->parse ("mains", "main");

Output
$tpl->p ("mains");

We can then view the source file to make sure that the contents of the Header.html,footer.html two child template files have been added to the main template.

<!--This is the page head-->
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
Here is a list
<UL>
<!--BEGIN list-->
<li> 's achievement is
<!--end list-->
</UL>
<!--This is the foot of the page-->
<p>author©iwind
</BODY>
</HTML>

You will find that all the variables are gone, including our unassigned,,. this is because when we create the object, the second parameter is not set, and the "remove" is automatically adopted.
$TPL = new Template ("Template");
And
$TPL = new Template ("Template", "remove");
The effect is the same.

If we want to assign values to these variables, then the method is the same as the analysis of variables in a single template.
Read template content into variables
$TPL->set_file ("main", "third.html");
$tpl->set_file ("My_header", "header.html");
$tpl->set_file ("My_footer", "footer.html");

Set the value of the variable title in the header.html of the child template
$tpl->set_var ("title", "This is the page title");

The following analysis of the contents of the block in the main template
Set blocks
$TPL->set_block ("main", "List", "lists");
$array = Array ("John" => 82, "Dick" => 90, "Wang er" => 60, "leper" => 77);
foreach ($array as $username => $score)
{
$TPL->set_var ("username", $username);
$tpl->set_var ("Score", $score);
$tpl->parse ("lists", "list", true);
}

All Programs are
<?php
Include in template class Template.inc
Require "inc/template.inc";

Create an instance
$TPL = new Template ("Template");

Read the whole file in
$TPL->set_file ("main", "third.html");
$tpl->set_file ("My_header", "header.html");
$tpl->set_file ("My_footer", "footer.html");

Set the value of the variable title in the header.html
$tpl->set_var ("title", "This is the page title");

Set blocks
$TPL->set_block ("main", "List", "lists");
$array = Array ("John" => 82, "Dick" => 90, "Wang er" => 60, "leper" => 77);
foreach ($array as $username => $score)
{
$TPL->set_var ("username", $username);
$tpl->set_var ("Score", $score);
$tpl->parse ("lists", "list", true);
}

Executes the template variable substitution in the My_header,my_footer and assigns the final result to the Header,footer in the main template, respectively.
$tpl->parse ("header", "My_header");
$tpl->parse ("Footer", "My_footer");

Complete the substitution of variables within the main template
$tpl->parse ("mains", "main");

Output
$tpl->p ("mains");

?>

The result of the output is
<!--This is the page head-->
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> This is the page title </TITLE>
</HEAD>
<BODY>
Here is a list
<UL>

<li> John's score is 82.
<li> Dick's score is 90.
<li> Wang's score is 60.
<li> the grade of the leper is 77.
</UL>
<!--This is the foot of the page-->
<p>author©iwind
</BODY>
</HTML>

Everything is what we expect.

In this program, we use
$TPL->set_block ("main", "List", "lists");
Loads a block. In fact, its first argument is the parent variable of the block, and if the block is in the header.html, then I'm afraid it's going to be written.
$tpl->set_block ("My_header", "List", "lists");
But the analytical approach is the same.

From the past and the examples in this section, we can see that the way to define a template variable value is to use the
$tpl->set_var ("Var_name", "Var_value");
But it takes parse to give the value of a variable to another variable.
$tpl->parse ("Target_name", "From_name", true);
Or
$tpl->parse ("Target_name", "From_name", false);
Use parse to perform the substitution of the template variable of from_name large variable, and then assign the result to Target_name.

A variable is defined in the same way wherever it is in the template (inside the block, in the child template).

A child template can also embed a new child template, called "Multiple nesting", the analytical methods are the same, but generally not used. Blocks can also be nested and useful so that templates can be designed to be very clear, which is what we'll do in the next section.

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.