4 * @package News_Templates
5 * @copyright Copyright (c) 2008, Bellière Ludovic
6 * @license http://opensource.org/licenses/mit-license.php MIT license
9 class templates_exception
extends Exception
{}
12 * @author Bellière Ludovic
14 * @package News_Templates
15 * @copyright Copyright (c) 2008, Bellière Ludovic
16 * @license http://opensource.org/licenses/mit-license.php MIT license
24 protected $_files = array();
27 * Path to the templates files
31 protected $_templatePath;
33 private $_escape = array('htmlentities');
35 public function __construct($template_path='./templates/') {
36 $this->_templatePath
= $template_path;
40 * Return the current tempalte path
42 public function getTemplatePath() {
43 return $this->_templatePath
;
49 public function addFile($tag,$name) {
50 $this->_files
[$tag] = $name;
54 * Process the templates files by $tag
55 * $tag can be an array for multi-templates page.
57 * @param array|string $tag
59 public function render($tag) {
61 if (!is_array($tag)) {
63 if (isset($this->_files
['_begin'])) {
64 include $this->_file('_begin');
66 include $this->_file($tag);
67 if (isset($this->_files
['_end'])) {
68 include $this->_file('_end');
70 //return ob_end_clean();
73 if (isset($this->_files
['_begin'])) {
74 include $this->_file('_begin');
78 foreach ($tags as $tag) {
79 include $this->_file($tag);
81 if (isset($this->_files
['_end'])) {
82 include $this->_file('_end');
86 } catch (templates_exception
$e) {
87 die ($e->getMessage());
88 } catch (Exception
$e) { die('ex error: '.$e->getMessage()); }
92 * Check if the template file is readable and returns its name
96 private function _file($tag) {
97 if (is_readable($this->_templatePath
.$this->_files
[$tag])) {
98 return $this->_templatePath
.$this->_files
[$tag];
100 throw new templates_exception('The file <em>'.$this->_templatePath
.$this->_files
[$tag]. '</em> is not readable');
105 * Used to set some functions who escape the content
107 * @param function $ref
109 public function setEscape($ref) {
110 if (!in_array($ref,$this->_escape
)) {
111 $this->_escape
[] = $ref;
116 * Used to remove a function from the pool of escape
118 public function remEscape($ref,$id=false) {
119 if ($id && isset($this->_escape
[$id])) {
120 unset($this->_escape
[$id]);
123 foreach ($this->_escape
as $key => $val) {
125 unset($this->_escape
[$key]);
133 public function escape($var) {
134 foreach($this->_escape
as $fnct) {
135 if (in_array($fnct, array('htmlentities','htmlspecialchars'))) {
136 $var = call_user_func($fnct, $var, ENT_COMPAT
, 'utf8');
138 $var = call_user_func($fnc, $var);
144 public function __isset($key) {
145 $strpos = strpos($key,'_');
146 if (!is_bool($strpos) && $strpos !== 0) {
147 return isset($this->$key);