Home arrow PHP arrow Page 5 - Simple PHP Templates With PatTemplate
PHP

Simple PHP Templates With PatTemplate


PatTemplate is a template system that gives us the power and flexibility to add templates to our PHP driven sites. In this article Havard gives us details and examples of how to use patTemplate with PHP.

Author Info:
By: Havard Lindset
Rating: 5 stars5 stars5 stars5 stars5 stars / 217
June 26, 2002
TABLE OF CONTENTS:
  1. · Simple PHP Templates With PatTemplate
  2. · An Overview Of PatTemplate
  3. · How to Process Template In PHP
  4. · Different Template Types
  5. · Template Visibility
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials