[Lambda-cvs] lambda/classes lambdaTemplate.class.php,1.3,1.4
Status: Pre-Alpha
Brought to you by:
ariejan
From: adam m. <ada...@us...> - 2004-06-22 12:56:52
|
Update of /cvsroot/lambda/lambda/classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11100/classes Modified Files: lambdaTemplate.class.php Log Message: Added lambdaTemplate class Index: lambdaTemplate.class.php =================================================================== RCS file: /cvsroot/lambda/lambda/classes/lambdaTemplate.class.php,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** lambdaTemplate.class.php 19 Jun 2004 10:25:22 -0000 1.3 --- lambdaTemplate.class.php 22 Jun 2004 12:56:44 -0000 1.4 *************** *** 3,7 **** /** * @package Lambda ! * @author Ariejan de Vroom <ar...@la...> * @version $Id$ * @copyright Copyright (C) 2004 Project Lambda --- 3,7 ---- /** * @package Lambda ! * @author Adam McMaster <ad...@la...> * @version $Id$ * @copyright Copyright (C) 2004 Project Lambda *************** *** 10,45 **** /** ! * Handles HTML output * @package lambdaCore **/ class lambdaTemplate extends lambdaObject { ! /** ! * Sets the classname ! **/ ! function __construct() { ! $this->setClassName("template"); ! } ! ! /** ! * Show HTML header ! **/ ! function showHeader() { ! print "<html>\n" ! ."<head>\n" ! ."<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n" ! ."<title>Project Lambda</title>\n" ! ."<link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\" />\n" ! ."</head>\n" ! ."<body>\n"; ! } ! ! /** ! * Show HTML footer ! **/ ! function showFooter() { ! print "</body>" ! ."</html>"; ! } } --- 10,115 ---- /** ! * Handles data output * @package lambdaCore **/ class lambdaTemplate extends lambdaObject { ! /** ! * @var array Keys (tags) which will be replaced in this template, of the form {KEY} ! **/ ! private $keys; ! ! /** ! * @var array The strings that the keys will be replaced with ! **/ ! private $values; ! ! /** ! * @var string The (unprocessed) template data ! **/ ! private $templateData; ! ! /** ! * @var object Error handler reference ! **/ ! private $handler; ! ! /** ! * Sets the classname, sets up error handler ! **/ ! function __construct() { ! global $handler; ! $this->handler = $handler; ! $this->setClassName('template'); ! } + /** + * Adds a new template key/value pair + * + * @return bool TRUE if successful, FALSE if key already exists + **/ + function addPair($key,$value) { + if(!in_array($key,$this->keys)) { + array_push($this->keys,'/\{'.$key.'\}/'); /* Surround key with { and }, and the key*/ + /* must be in the form of a regex, /{$key}/ */ + array_push($this->values,$value); + return TRUE; + }else{ + $this->handler->warning('Key value "'.$key.'" already exists in this template, not adding again'); + return FALSE; + } + } + + /** + * Removes a key/value pair + * + * @return bool TRUE on success, FALSE if the key was not found + **/ + function delPair($key) { + if($keyID = array_search($key,$this->keys)) { + $this->keys[$keyID] = $this->values[$keyID] = NULL; + return TRUE; + }else{ + $this->handler->warning('Key "'.$key.'" not found in this template'); + return FALSE; + } + } + + /** + * Appends raw template data to $templateData from a template file + * + * This will first attempt to open $file for reading, if that fails + * an error will be displayed and the script will stop. Next the + * contents of the file are read, and again if that fails the script + * will stop after an error is displayed. + * + * For example, you could add head.tpl followed by body.tpl then + * foot.tpl; you can pick which template parts you need for a particular page. + * + * @return bool TRUE on success, FALSE on failure + **/ + function addFile($file) { + if($fp = fopen(LAMBDA_TEMPLATE . $file,r)) { + if($data = fread($fp,filesize(LAMBDA_TEMPLATE . $file))) { + $this->templateData .= $data; /* Append data to $templateData */ + fclose($fp); + return TRUE; + }else{ + fclose($fp); + $this->handler->error('Unable to read template data',TRUE); + return FALSE; /* In practice the script is killed before this, but */ + /* still return for completeness */ + } + }else{ + $this->handler->error('Unable to open template for reading',TRUE); + return FALSE; + } + } + + /** + * Prints template with data inserted into it + **/ + function output() { + print preg_replace($keys,$values,$templateData); + } } |