Phplib template entry-4 template nesting

Source: Internet
Author: User

Introduction: This is a detailed page of The phplib template getting started series-4 template nesting. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324604 'rolling = 'no'>

In PHPProgram, We often put "PublicCode"Or" public part "is written into a file. The former is like our system configuration file, such as config. both PHP and public functions write a function. in the PHP file, the latter is like the header and tail of the page that a site needs. the advantage of doing so is that you can easily maintain the site, and if this public part needs to be changed, you do not need to modify every page, greatly reducing our workload.

Phplib template entry-4 template nesting
[Main floor]
In PHP programs, we often write "Public Code" or "public part" into a file. The former is like our system configuration file, such as config. both PHP and public functions write a function. in the PHP file, the latter is like the header and tail of the page that a site needs. the advantage of doing so is that you can easily maintain the site, and if this public part needs to be changed, you do not need to modify every page, greatly reducing our workload.
In the past, you may use require, include (require_once, include_once) to introduce a public page header, which is indeed convenient and effective. Now we can use the template class to implement it, you can easily insert a page to any part of another template. if you want to make the page to be inserted into a template containing variables, you will find that the template class will handle the work well.

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

<Body>
Below is a list
<Ul>
<! -- Begin list -->
<Li> the score is
<! -- End list -->
</Ul>
<! -- This is the page foot -->

</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 the previous sections use
$ TPL-> set_file ("Main", "template file name ");
To load the template file. The name is only "Main" because we want to give it a meaning: The main template. "Sub-template". Similarly, footer.html is also a "sub-template". We want to place it in the main template. A master template can be embedded with any number of sub-templates with unlimited content, size, and format.

Next we will start our PHP program.

First, create an Instance Object of the class
// Include the template. inc
Require "INC/template. Inc ";

// Create an instance
$ TPL = new template ("template ");

// Read the content of the three template files and give the variables "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 ");

// Execute my_header, replace the template variable in my_footer, and assign the final result to the header and footer in the main template respectively.
$ TPL-> parse ("Header", "my_header ");
$ TPL-> parse ("footer", "my_footer ");

// Replace the variables in the main template and output the analyzed content of the main template.
$ TPL-> parse ("Mains", "Main ");

// Output
$ TPL-> P ("Mains ");

Therefore, the contents of the source file header.html and footer.html have been added to the main template.

<! -- This is the page header -->
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> </title>
</Head>
<Body>
Below is a list
<Ul>
<! -- Begin list -->
<Li> the score is
<! -- End list -->
</Ul>
<! -- This is the page foot -->
<P> author iwind
</Body>
</Html>

You will find that all the variables are missing, including those we did not assign a value, because the second parameter is not set when we create an object, and the "Remove" is automatically used"
$ TPL = new template ("template ");
And
$ TPL = new template ("template", "Remove ");
The effect is the same.

If we want to assign values to these variables, the method is the same as the variable analysis method in a single template.
// Read the template content into the variable
$ TPL-> set_file ("Main", "third.html ");
$ TPL-> set_file ("my_header", "header.html ");
$ TPL-> set_file ("my_footer", "footer.html ");

// Set the title value of the variable in the subtemplate header.html.
$ TPL-> set_var ("title", "this is the webpage title ");

// Analyze the block content in the main template as follows
// Set the block
$ TPL-> set_block ("Main", "list", "Lists ");
$ Array = array ("Zhang San" => 82, "Li Si" => 90, "Wang 'er" => 60, "Ma Zi" => 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 the template. inc
Require "INC/template. Inc ";

// Create an instance
$ TPL = new template ("template ");

// Read the entire file
$ TPL-> set_file ("Main", "third.html ");
$ TPL-> set_file ("my_header", "header.html ");
$ TPL-> set_file ("my_footer", "footer.html ");

// Set the title value of the variable in header.html
$ TPL-> set_var ("title", "this is the webpage title ");

// Set the block
$ TPL-> set_block ("Main", "list", "Lists ");
$ Array = array ("Zhang San" => 82, "Li Si" => 90, "Wang 'er" => 60, "Ma Zi" => 77 );
Foreach ($ array as $ username => $ score)
{
$ TPL-> set_var ("username", $ username );
$ TPL-> set_var ("score", $ score );
$ TPL-> parse ("Lists", "list", true );
}

// Execute my_header, replace the template variable in my_footer, and assign the final result to the header and footer in the main template respectively.
$ TPL-> parse ("Header", "my_header ");
$ TPL-> parse ("footer", "my_footer ");

// Replace the variables in the main Template
$ TPL-> parse ("Mains", "Main ");

// Output
$ TPL-> P ("Mains ");

?>

The output result is
<! -- This is the page header -->
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> This is the webpage title </title>
</Head>
<Body>
Below is a list
<Ul>

<Li> Michael Jacob's score is 82.
<Li> Li Si's score is 90.
<Li> Wang's score is 60.
<Li> Ma Zi's score is 77.
</Ul>
<! -- This is the page foot -->
<P> author iwind
</Body>
</Html>

Everything is what we expect.

In this program, we use
$ TPL-> set_block ("Main", "list", "Lists ");
When the first character of a batch is the parent volume of the batch. If this character is in header.html, I'm afraid it will be written like this.
$ TPL-> set_block ("my_header", "list", "Lists ");
However, the analysis method is the same.

From the past and examples in this section, we can see that the method for defining a template variable value is to use
$ TPL-> set_var ("var_name", "var_value ");
However, parse is required 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 replace the template variable of the big variable from_name, and then assign the result to target_name.

A variable is defined in the same way no matter where it is in the template (inside the block, in the sub-template.

The sub-template can also be embedded into a new sub-template, called "multiple nesting". The analysis methods are the same, but they are generally not used. blocks can also be nested and very useful, so that the template can be clearly designed. This is what we will discuss in the next section.

More articles on "phplib template entry-4 template nesting"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324604.html pageno: 14

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.