| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Copyright (c) 2003 Brian E. Lozier (brian@massassi.net)
|
| 4 |
*
|
| 5 |
* set_vars() method contributed by Ricardo Garcia (Thanks!)
|
| 6 |
*
|
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
* of this software and associated documentation files (the "Software"), to
|
| 9 |
* deal in the Software without restriction, including without limitation the
|
| 10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
| 11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
| 12 |
* furnished to do so, subject to the following conditions:
|
| 13 |
*
|
| 14 |
* The above copyright notice and this permission notice shall be included in
|
| 15 |
* all copies or substantial portions of the Software.
|
| 16 |
*
|
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
| 23 |
* IN THE SOFTWARE.
|
| 24 |
*/
|
| 25 |
|
| 26 |
class Template {
|
| 27 |
var $vars; /// Holds all the template variables
|
| 28 |
var $path; /// Path to the templates
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Constructor
|
| 32 |
*
|
| 33 |
* @param string $path the path to the templates
|
| 34 |
*
|
| 35 |
* @return void
|
| 36 |
*/
|
| 37 |
function Template($path = null) {
|
| 38 |
$this->path = $path;
|
| 39 |
$this->vars = array();
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Set the path to the template files.
|
| 44 |
*
|
| 45 |
* @param string $path path to template files
|
| 46 |
*
|
| 47 |
* @return void
|
| 48 |
*/
|
| 49 |
function set_path($path) {
|
| 50 |
$this->path = $path;
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Set a template variable.
|
| 55 |
*
|
| 56 |
* @param string $name name of the variable to set
|
| 57 |
* @param mixed $value the value of the variable
|
| 58 |
*
|
| 59 |
* @return void
|
| 60 |
*/
|
| 61 |
function set($name, $value) {
|
| 62 |
$this->vars[$name] = $value;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Set a bunch of variables at once using an associative array.
|
| 67 |
*
|
| 68 |
* @param array $vars array of vars to set
|
| 69 |
* @param bool $clear whether to completely overwrite the existing vars
|
| 70 |
*
|
| 71 |
* @return void
|
| 72 |
*/
|
| 73 |
function set_vars($vars, $clear = false) {
|
| 74 |
if($clear) {
|
| 75 |
$this->vars = $vars;
|
| 76 |
}
|
| 77 |
else {
|
| 78 |
if(is_array($vars)) $this->vars = array_merge($this->vars, $vars);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Open, parse, and return the template file.
|
| 84 |
*
|
| 85 |
* @param string string the template file name
|
| 86 |
*
|
| 87 |
* @return string
|
| 88 |
*/
|
| 89 |
function fetch($file) {
|
| 90 |
extract($this->vars); // Extract the vars to local namespace
|
| 91 |
ob_start(); // Start output buffering
|
| 92 |
include($this->path . "/$file"); // Include the file
|
| 93 |
$contents = ob_get_contents(); // Get the contents of the buffer
|
| 94 |
ob_end_clean(); // End buffering and discard
|
| 95 |
return $contents; // Return the contents
|
| 96 |
}
|
| 97 |
}
|
| 98 |
?>
|