PHPBuilder - Templates: The PHPLib Way Page 3

RSS Twitter

Templates: The PHPLib Way - Page 3

by: David Orr
July 30, 2000

Nested Templates

A neat feature of the parse() function is that the MyOutput handle that it created is actually a template variable, just as some_color is a template variable. So if you have another template with a {MyOutput} tag, when you parse that second template, all of the {MyOutput} tags will be replaced with the parsed text from MyOutput. This lets you embed the text of one template file into another template. So, we could have another template called wholePage.ihtml that contains the text:
Sorry you didn't win. But if you had won, we would have told you: {MyOutput}
And after wholePage.ihtml is parsed, the final output would be:
Sorry you didn't win. But if you had won, we would have told you: Congratulations! You won a new blue Honda Prelude!
Here is the PHP code to parse both templates:


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

// These three lines are the same as the first example:
// (Note that we don't call p() 
//here, so nothing gets output yet.)

// Now parse a second template:
// wholePage.ihtml has "{MyOutput}" in it
// All {MyOutput}'s get replaced
// output the value of MyFinalOutput

The last two lines with calls to parse() and p() can actually be combined using the shorthand function pparse() by replacing the last two lines with :

Another feature of PHPLIB Template is that the functions set_file() and set_var() can also accept multiple sets of values at a time by passing an array of handle/value pairs. Here are examples:


"pageOneHandle" => "pageone.ihtml",
"pageTwoHandle" => "pagetwo.ihtml"));

"last_name" => "Gates",
"first_name" => "Bill",
"net_worth" => $reallybignumber));


« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

David Orr



(Maximum characters: 1200). You have characters left.