Template, Phplib processing mode (II.)

Source: Internet
Author: User
Template nesting Template




A smart feature of the
parse () function is that the myoutput handle it creates is a real template variable, just as Some_color is a


a template variable. So if you have another template, it has a {myoutput} tag, and when you analyze the second template, all the

The
{myoutput} tag will be replaced with the parsed text in Myoutput. This feature allows you to embed a template file into another template

In
. So, we might have another template named Wholepage.ihtml, which reads as follows:





Sorry, you didn't win. But if you win, we'll say to you:


{Myoutput}





and after the wholepage.ihtml is analyzed, the final result will be:





Sorry, you didn't win. But if you win, we'll say to you:


Congratulations! You won a new blue Honda prelude!.





the following is a PHP code that analyzes two templates:





<?php





$t = new Template ("/home/mydir/mytemplates/");





//These three lines are the same as the first example


$t->set_file ("Myfilehandle", "mytemplate.ihtml");


$t->set_var ("Some_color", $my _color);


$t->parse ("Myoutput", "Myfilehandle");





//Note we did not call P ()


//Here, there is still no output of anything





Now analyze the second template


$t->set_file ("Wholehandle", "wholepage.ihtml");





//Wholepage.ihtml has "{myoutput}" inside


$t->parse ("Myfinaloutput", "Wholehandle");





//All {myoutput} was replaced


$t->p ("Myfinaloutput");


//Output Myfinaloutput value





?>


finally called the two-line statements of parse () and P () to be merged into a shorthand function pparse ():





pparse ("Myfinaloutput", "Secondhandle");

The
of another phplib template is that the Set_file () and Set_var () functions can also receive multiple sets of values at a time by passing a handle/array


an array of arrays. This is an example:


<?php


$t->set_file (Array ("Pageonehandle" => "pageone.ihtml",


"Pagetwohandle" => "pagetwo.ihtml"));


$t->set_var (Array ("Last_Name" => "Gates",


"first_name" => "Bill",


"Net_worth" => $reallybignumber));


?>


Add template text





You can pass a third argument to parse () and Pparse () if you want to append data to the template variable instead of replacing it. can be simply


uses true as the third parameter to invoke parse () and Pparse (), such as: <?php


$t->parse ("Myoutput", "Myfilehandle", true);


?>


If the myoutput already contains the data, Myfilehandle will be parsed and appended to the myoutput already existing data. This technique is very useful, if you already have a template, you want the same piece of text to be repeated multiple times, such as listing multiple rows in a database query result. You may also be displaying the variables in the array, as in the following example:


<?php


$t = new Template ("/home/mydir/mytemplates/"); $t->set_file (Array ("MainPage" => "mainpage.ihtml", "Each_element" => "each_element.ihtml")), Reset ($myArray while ($elementName, $elementValue) = each ($myArray)) {


//Set ' value ' and ' name ' for the values and names of each element


$t->set_var ("name", $elementName); $t->set_var ("value", $elementValue);


//Append each_element copy


$t->parse ("Array_elements", "Each_element", True);} $t->pparse ("Output", "mainpage"); >


This example uses two templates, mainpage.ihtml and each_element.ihtml. mainpage.ihtml template may be like this:








is the array:





{array_elements}




The {array_elements} tag above the
will be replaced by a each_element.ihtml copy, which will be heavy based on the array ($myArray)


Complex. The each_element.ihtml template may look like this:





{Name}: {Value}




The result of
processing is a well-formed table containing $myarray elements. But it's better if you combine two templates into one template


? In fact, they can be combined using template blocks. The template block allows you to remove a piece of text from a template, so you can repeat it very


many times, or do whatever you want on it. But I will cover this feature in another article.








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.