Logo         Community
The Company
Your Account
Contact Us
Template-Based Web Development With patTemplate (part 2)
Take things to the next level with information on patTemplate's more sophisticated features

| Scoping it Down |

You've already seen how to use the AddVar() method to replace template variables with actual values. However, template variables are "local" to a template, and so, the value assigned to a template variable cannot be accessed from other templates (actually, that's a little white lie which I'm going to recant on the next page, but bear with me for a moment).

In the event that you need a variable which is "global", and whose value can be accessed from more than one template, patTemplate offers the addGlobalVar() method call, which makes the value of a template variable available to all other templates.

Take a look at the following templates, all of which use the template variable {IMAGES}:

<!-- app1.tmpl -->
<patTemplate:tmpl name="main">
<basefont face="Arial">


<table border="1" cellspacing="0" cellpadding="0">

<patTemplate:link src="top" />

<patTemplate:link src="middle" />

<patTemplate:link src="bottom" />



<patTemplate:tmpl name="top">
<td colspan="3"><img src="{IMAGES}/top.gif"></td>

<patTemplate:tmpl name="middle">
<td valign="top"><img src="{IMAGES}/left.gif"></td>
<form action="login">
<table border="0" cellspacing="4" cellpadding="0">
<td><font size="-1">Username:</font></td>
<td><input type="text" size="6"></td>
<td><font size="-1">Password:</font></td>
<td><input type="password" size="6"></td>
<td valign="top"><img src="{IMAGES}/right.gif"></td>

<patTemplate:tmpl name="bottom">
<td colspan="3"><img src="{IMAGES}/bottom.gif"></td>

Now, rather than making three calls to AddVar() - one for each of the templates referencing the variable {IMAGES} - I can save myself time with a single call to AddGlobalVar(), which makes the value of {IMAGES} available in the global namespace. Here's the script:


// include the class

// initialize an object of the class
$template = new patTemplate();

// set template location

// add templates to the template engine

// add global variable
$template->AddGlobalVar("IMAGES", "/app1/images");

// parse and display the template

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback