PHPBuilder - Templates - why and how to use them in PHP3 Page 2



RSS Twitter
Articles Html

Templates - why and how to use them in PHP3 - Page 2

by: Sascha Schumann
|
July 30, 2000

Now you want FastTemplate to substitute all your {MACROS} with their respective values in the template
foo
Issue the command
<?php $tpl->parse(PAGECONTENT"foo"); ?>
to do that. This command will assign the content of the template "foo" to the unique name PAGECONTENT.
Of course, we are not done with that yet since the template
bar
holds the main page definition and FastTemplate needs to substitute the macro {PAGECONTENT} in it. We also need to assign a PAGETITLE and to do that we say:

<?php

$tpl
->assign(PAGETITLE"FooBar test");
$tpl->parse(MAIN"bar");

?>
Easy, is it not? We just need to print it out now:

<?php

$tpl
->FastPrint(MAIN);

?>
The following three files show how the more verbose description looks in practice. I would not know how to live without this technique in real life - your designers will be happy and your boss will smile, because you can do more things in less time.
bar.tpl
<!-- bar.tpl -->
<HTML>
<HEAD><TITLE>Feature world - {PAGETITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGETITLE}</H1>
{PAGECONTENT}
</BODY>
</HTML>
foo.tpl
<!-- foo.tpl -->
This does not do anything obvious. Please look at {NAME}.
demo.php3

<?php

include "class.FastTemplate.php3";

$tpl = new FastTemplate(".");
$tpl->define(array(foo => "foo.tpl"bar => "bar.tpl"));

$tpl->assign(NAME"me");
$tpl->assign(PAGETITLE"Welcome!");

$tpl->parse(PAGECONTENT"foo");
$tpl->parse(MAIN"bar");

$tpl->FastPrint(MAIN);

?>
Building whole tables
I have also written a short example which demonstrates how to build whole tables with single row templates. It is effective, because you still do not need to tinker with HTML directly.
We attach the content of a template to an already defined unique name to build the HTML table. This can be done by sticking a single "." into the front of the template name when calling $tpl->parse():

<?php

# assign the content of template foo to the unique name TPL1
$tpl->parse(TPL1"foo");

# attach the content of template bar to the unique name TPL1
$tpl->parse(TPL1".bar");

?>

« Previous Page
1
|
2
|
3
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Sascha Schumann

Comment:



Comment:

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