include("../class/class.ASAPlate.php");
$tpl = new ASAPlate(".");
$tpl->Load("main.html");
$tpl->Load("menu.html");
$tpl->Load("extra.html");
$tpl->Load("body.html");
if($mul==""){
$mul=1;
}
$tpl->Assign(TITLE, "I am the title");
$tpl->Assign(MULTIP, $mul);
for($a=1;$a<=10;$a++){
$tpl->assign(( array( NUMBER => $a,
RESULT => $a*$mul )));
$tpl->Parse(TABLA, TABLAS, true);
}
$tpl->Parse(BODY);
$tpl->Parse(MAIN);
$tpl->FastPrint(MAIN);
// Uncoment this to watch the internal working of EasyTemplate
/* echo("--------------------------------
");
reset($tpl->PARSEVARS);
while (list ($LineNumber, $Line) = each ($tpl->PARSEVARS)) {
echo "$LineNumber=",htmlspecialchars($Line),"
";
}*/
?>
=============
¼Ò ½º
=============
/***************************************************************************
* class.ASAPlate.php *
* this class that makes very easy to separate php source code from the *
* templates that generate the look and feel of a web page. *
* ------------------- *
* begin : 2002/01/03 *
* author : Jos?Pablo Ezequiel (Pupeno) Fern?dez *
* email : pupeno@pupeno.com.ar *
* versi?: 0.0.1
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/***************************************************************************
* This work is based on the class FastTemplate that you can find at *
* http://www.thewebmasters.net/php/FastTemplate.phtml and a modification *
* made to that class by Sebastian Grignoli *
***************************************************************************/
class ASAPlate {
/***************************************************************************
* Variables *
***************************************************************************/
var $ROOT = ""; // Holds path-to-templates
var $WIN32 = false; // Set to true if this is a WIN32 server
var $STRICT = true; // Strict template checking. Unresolved
vars in templates will generate a warning when found.
var $ERROR = ""; // Holds the last error message
var $PARSEVARS = array(); // Holds the array of Variable handles
PARSEVARS[HANDLE] == "value"
/***************************************************************************
* Constructor *
* Returns: EasyTemplate object *
* $PathToTemplates: where the templates could be found *
***************************************************************************/
function ASAPlate ($PathToTemplates = ""){
global $PHPErrorMsg;
if(!empty($PathToTemplates)){
$this->SetRoot($PathToTemplates);
}
} // end (new) EasyTemplate ()
/***************************************************************************
* All templates will be loaded from this "root" directory *
* Can be changed in mid-process by re-calling with a new *
* value. *
* $Root: the path to be root of the templates *
***************************************************************************/
function SetRoot ($Root){
$Trailer = substr($Root,-1);
if(!$this->WIN32){
if( (ord($Trailer)) != 47 ){
$Root = $Root.chr(47); // Let``s add the trailing "/" if it``s not there
}
if(is_dir($Root)){
$this->ROOT = $Root;
} else {
$this->ROOT = "";
$this->error("Specified ROOT dir [$Root] is not a directory");
}
} else {
// WIN32 box - no testing
if( (ord($trailer)) != 92 )
{
$Root = $Root.chr(92);
}
$this->ROOT = $Root;
}
}
// Strict and non strict ways are not yet implemented.
/***************************************************************************
* Strict template checking, if true sends warnings to STDOUT when *
* parsing a template with undefined variable references *
* Used for tracking down bugs-n-such. Use no_strict() to disable. *
***************************************************************************/
function Strict(){
$this->STRICT = true;
}
/***************************************************************************
* Silently discards (removes) undefined variable references *
* found in templates *
***************************************************************************/
function NoStrict (){
$this->STRICT = false;
}
/***************************************************************************
* A quick check of the template file before reading it. *
* This is -not- a reliable check, mostly due to inconsistencies *
* in the way PHP determines if a file is readable. *
***************************************************************************/
function IsSafe($FileName){
if(!file_exists($FileName)){
$this->Error("[$FileName] does not exist",0);
return false;
}
return true;
}
/**************************************************************************
* Show the error and exit if necesary *
* $ErrorMsg: Error Message to be printed *
* $Die: if it``s 1, it``s a fatal error and the script ends, otherwise it *
* doesn``t *
**************************************************************************/
function Error($ErrorMsg, $Die = 0){
$this->ERROR = $ErrorMsg;
printf("ERROR: $this->ERROR
");
if ($Die == 1){
exit;
}
return;
} // end Error()
/**************************************************************************
* Grabs a template from the root dir and *
* reads it into a (potentially REALLY) big string *
* Returns: false in case the file can``t be found, tru after opening *
* and parsing it. *
* $Template: The name of the file to be loaded *
**************************************************************************/
function Load($Template){
if(empty($this->ROOT)){
$this->Error("Cannot open template. Root not valid.",1);
return false;
}
$FileName = "$this->ROOT"."$Template";
/* if(!(IsSafe($FileName))){ CHECK WHY THIS DOESN``T WORK
return false;
}*/
$Contents = @file($FileName);
while (list ($LineNumber, $Line) = each ($Contents)) {
if (eregi("", $Line)){
$Section = "";
}
if($Section != ""){
$this->PARSEVARS["$Section"]=$this->PARSEVARS["$Section"].$Line;
}
if (eregi("", $Line)){
$TempVar = explode(" ", $Line);
$Section = $TempVar[3];
}
}
return true;
}
/***************************************************************************
* Assigns a value to a key for parsing, or an array *
* of values to keys. *
* $TPLArray: it could be a key for a value or an array with keys an values*
* $Trailer: it``s the value for a key when $TPLArray is not an array. *
***************************************************************************/
function Assign ($TPLArray, $Trailer=""){
if(gettype($TPLArray) == "array"){
while ( list ($Key,$Val) = each ($TPLArray) ){
if (!(empty($Key))){
// Empty values are allowed
// Empty Keys are NOT
$this->PARSEVARS["$Key"] = $Val;
}
}
} else {
// Empty values are allowed in non-array context now.
if (!empty($TPLArray)){
$this->PARSEVARS["$TPLArray"] = $Trailer;
}
}
}
/**************************************************************************
* Return the value of an assigned variable. *
* Christian Brandel cbrandel@gmx.de *
* Returns: the value of the given key *
* $TPLName: key for a value on the PARSEVARS array *
**************************************************************************/
function GetAssigned($TPLName = ""){
if(empty($TPLName)){
return false;
}
if(isset($this->PARSEVARS["$TPLName"])){
return ($this->PARSEVARS["$TPLName"]);
} else {
return false;
}
}
/***************************************************************************
* Parse a template *
* $Orig: the name of the template already on memory to be parsed. *
* $Dest: if it``s set, where the parsed template will be stored. *
* $Add: if it``s true, add instead of replacing the parsed template. *
***************************************************************************/
function Parse($Orig, $Dest="", $Add=false){
reset($this->PARSEVARS);
if($Dest == ""){
$Dest = $Orig;
}
$Temp = $this->PARSEVARS[$Orig];
while ( list ($Key,$Val) = each ($this->PARSEVARS) ){
if($Key==$Template){
continue;
}
if (!(empty($Key))){
if(gettype($Val) != "string"){
settype($Val,"string");
}
// php4 doesn``t like ``{$`` combinations.
$Key = ``{``."$Key".``}``;
$Temp = str_replace("$Key","$Val","$Temp");
}
}
if($Add){
$this->PARSEVARS[$Dest] = $this->PARSEVARS[$Dest].$Temp;
} else {
$this->PARSEVARS[$Dest] = $Temp;
}
}
/***************************************************************************
* FastPrint *
* $Template: the template to be printed *
***************************************************************************/
function FastPrint($Template){
if(!isset($this->PARSEVARS[$Template])){
$this->Error("Template [$Template] doesn``t exist", 1);
} else {
echo($this->PARSEVARS[$Template]);
}
}
}
?>