HomePHP Page 6 - Template-Based Web Development With patTemplate (part 2)
Hide And Seek - PHP
Got the basics down? Well, here's the advanced course - thisarticle demonstrates some of patTemplate's more sophisticated features,including the ability to dynamically show or hide templates, inheritvariables, use loops and conditional branches, and create dynamic,template-based forms and error handlers.
You can alter the visibility of a particular template via its "visibility" attribute. Consider the following example:
<patTemplate:tmpl name="main">
I spy, with my little eye...
<br>
<patTemplate:link src="toaster" />
</patTemplate:tmpl>
<patTemplate:tmpl name="toaster" visibility="hidden">
...a template beginning with T
</patTemplate:tmpl>
Here's the script:
<?php
// include the class
include("include/patTemplate.php");
// initialize an object of the class
$template = new patTemplate();
// set template location
$template->setBasedir("templates");
// add templates to the template engine
$template->readTemplatesFromFile("toaster.tmpl");
// parse and display the template
$template->displayParsedTemplate("main");
?>
And here's the output:
I spy, with my little eye...
Since the "visibility" attribute of the second template is
set to "hidden", it will never be displayed by the template engine. In order to display it, you'll need to turn visibility to "visible" (or just remove the "visibility" attribute altogether):
<?php
// include the class
include("include/patTemplate.php");
// initialize an object of the class
$template = new patTemplate();
// set template location
$template->setBasedir("templates");
// add templates to the template engine
$template->readTemplatesFromFile("toaster.tmpl");
// turn visibility on for "toaster" template
// comment out the next line to have the template vanish
$template->setAttribute("toaster", "visibility", "show");
// parse and display the template
$template->displayParsedTemplate("main");
?>
And now the output will include that missing section:
I spy, with my little eye...
...a template beginning with T
As you will see, though this might not seem like a big deal
right now, it becomes extremely powerful when combined with the ability to programmatically alter the "visibility" attribute on the fly. That's discussed on the next page.