Template-Based Web Development With patTemplate (part 2) - Speaking In Tongues
(Page 3 of 14 )
Related, though in a tangential manner, to the material discussed on the previous page, is the "varscope" attribute. This attribute allows you to import the values of template variables from other templates into the current template, thereby reducing the number of calls you have to make to addVar(). Here's a quick example:
<patTemplate:tmpl name="main" varscope="lang">
Would you like me to speak with you in {LANG}? </patTemplate:tmpl>
<patTemplate:tmpl name="lang">
{LANG}
</patTemplate:tmpl>
And 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("lang.tmpl");
// define a value for the LANG variable in the "lang" template
$template->addVar("lang", "LANG", "Japanese");
// parse and display the "main" template
// since this template inherits values from "lang"
// the value of LANG in this template will automatically be set
$template->displayParsedTemplate("main");
?>
In this case, I have two templates, "main" and "lang". Both
contain references to the template variable {LANG}. However, although the template variable {LANG} has been assigned a value in the "lang" template, no such assignment has been made for the "main" template. Despite this, the "main" template will inherit the correct value from the "lang" template, via the "varscope" attribute specified in its opening tag.
This is clearly demonstrated in the output of the script above:
Would you like me to speak with you in Japanese?
Next: Looping The Loop >>
More PHP Articles
More By Team Melonfire, (c) Melonfire