Simple PHP Templates With PatTemplate - Template Visibility
(Page 5 of 6 )
There comes a time when you want to hide templates from being showed. This is done by adding the visbility attribute to a template tag. The visibility attribute takes two options, show (default) and hidden. Here is an example where the "content" template is hidden:
<patTemplate:tmpl name="body">
<html>
<head>
<title>An invisible template</title>
</head>
<body>
<patTemplate:tmpl name="content" visibility="hidden">
This text isn't actually visible.
</patTemplate:tmpl>
</body>
</html>
</patTemplate:tmpl> This feature wouldn't be too useful if you only could change it at design-time. That's where the setAttribute() function comes in handy. Let's take a look at some PHP code:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example9.tmpl.html");
// Make the template "content" visible
$tmpl->setAttribute("content", "visibility", "show");
// Parse and display template
$tmpl->displayParsedTemplate("body");
?> Try commenting out the setAttribute line and see what happens. This feature can be useful for error messages, etc.
Template linking There are a lot of other useful features included in patTemplate. One of these features is the ability to link templates. Here is an example of template linking:
<patTemplate:tmpl name="body">
<html>
<head>
<title>Template linking</title>
</head>
<body>
<patTemplate:link src="content" />
</body>
</html>
</patTemplate:tmpl>
<patTemplate:tmpl name="content">
This is the content. I will be displayed within the "body"-template.<br>
Feel free to use variables inside this template too. Like this:<br><br>
My name is {NAME}.
</patTemplate:tmpl> The link within the "body" template links to the template "content", and includes it. Pretty easy, eh? Linking templates can make things easier when you’re editing large template files.
Here is the PHP code I used:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example6.tmpl.html");
// Add the variable
$tmpl->addVar("content", "NAME", "Maria");
// Parse and display template
$tmpl->displayParsedTemplate("body");
?> Another option is linking an external file, like I've done below:
<patTemplate:tmpl name="body">
<html>
<head>
<title>Template linking</title>
</head>
<body>
<patTemplate:tmpl name="content" src="example7content.tmpl.html" />
</body>
</html>
</patTemplate:tmpl> Let's make another file:
This is the content. I will be displayed within the "body"-template.<br>
Feel free to use variables inside this template too. Like this:<br><br>
My name is {NAME}. The PHP code that we can use is shown below:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example7.tmpl.html");
// Add the variable
$tmpl->addVar("content", "NAME", "Maria");
// Parse and display template
$tmpl->displayParsedTemplate("body");
?> I usually split my templates up into three different files: Header, content and footer. This way I only have to update one file if I want to modify the header (instead of changing the header part in every single template-file).
Global variables So far we've only looked at patTemplate using local variables. There are two different ways to add a variable with patTemplate: through the local scope or through the global scope. Here's an example of global variables in action:
<patTemplate:tmpl name="body">
<html>
<head>
<title>Variable scope</title>
</head>
<body>
This is a link to the image <a href="{IMAGEFILE}">{IMAGEFILE}</a><br><br>
<patTemplate:tmpl name="new">
It's also applied to this template.. look: {IMAGEFILE}
</patTemplate:tmpl>
</body>
</html>
</patTemplate:tmpl> And here's the PHP-code:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example8.tmpl.html");
// Add the global variable
$tmpl->addGlobalVar("IMAGEFILE", "dummy-image.jpg");
// Parse and display template
$tmpl->displayParsedTemplate("body");
?> When you use the function addGlobalVar(), you add the value to all the matching variables in the loaded template-file.
Next: Conclusion >>
More PHP Articles
More By Havard Lindset