PHPBuilder - Templates: The PHPLib Way Page 3



RSS Twitter
Articles Html

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:

<?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 that we don't call p() 
//here, so nothing gets output yet.)

// Now parse a second template:
$t->set_file("WholeHandle","wholePage.ihtml"); 
    
// wholePage.ihtml has "{MyOutput}" in it
$t->parse("MyFinalOutput","WholeHandle"); 
    
// All {MyOutput}'s get replaced
$t->p("MyFinalOutput"); 
    
// 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 :

<?php 
pparse
("MyFinalOutput","SecondHandle"); 
?>
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:

<?php

$t
->set_file(array(
    
"pageOneHandle" => "pageone.ihtml",
    
"pageTwoHandle" => "pagetwo.ihtml"));

$t->set_var(array(
    
"last_name" => "Gates",
    
"first_name" => "Bill",
    
"net_worth" => $reallybignumber));

?>

« Previous Page
1
|
2
|
3
|
4
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
David Orr

Comment:



Comment:

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