adding comments and possibility to use "light" alternate format to display the news
[ayans.git] / includes / templates.php
blob2ac5caae70b054455f3594355b3de2581b3cf902
1 <?php
2 /**
3 * @category News
4 * @package News_Templates
5 * @copyright Copyright (c) 2008, Bellière Ludovic
6 * @license http://opensource.org/licenses/mit-license.php MIT license
7 */
9 class templates_exception extends Exception {}
11 /**
12 * @author Bellière Ludovic
13 * @category News
14 * @package News_Templates
15 * @copyright Copyright (c) 2008, Bellière Ludovic
16 * @license http://opensource.org/licenses/mit-license.php MIT license
18 class templates {
19 /**
20 * File list
22 * @var array
24 protected $_files = array();
26 /**
27 * Path to the templates files
29 * @var string
31 protected $_templatePath;
33 private $_escape = array('htmlentities');
35 public function __construct($template_path='./templates/') {
36 $this->_templatePath = $template_path;
39 /**
40 * Return the current tempalte path
42 public function getTemplatePath() {
43 return $this->_templatePath;
46 /**
47 * Add a template file
49 public function addFile($tag,$name) {
50 $this->_files[$tag] = $name;
53 /**
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) {
60 try {
61 if (!is_array($tag)) {
62 ob_start();
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();
71 } else {
72 ob_start();
73 if (isset($this->_files['_begin'])) {
74 include $this->_file('_begin');
76 $tags = $tag;
77 unset($tag);
78 foreach ($tags as $tag) {
79 include $this->_file($tag);
81 if (isset($this->_files['_end'])) {
82 include $this->_file('_end');
85 ob_end_flush();
86 } catch (templates_exception $e) {
87 die ($e->getMessage());
88 } catch (Exception $e) { die('ex error: '.$e->getMessage()); }
91 /**
92 * Check if the template file is readable and returns its name
94 * @param string $tag
96 private function _file($tag) {
97 if (is_readable($this->_templatePath.$this->_files[$tag])) {
98 return $this->_templatePath.$this->_files[$tag];
99 } else {
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]);
121 return true;
122 } elseif (!$id) {
123 foreach ($this->_escape as $key => $val) {
124 if ($val == $ref) {
125 unset($this->_escape[$key]);
126 return true;
130 return false;
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');
137 } else {
138 $var = call_user_func($fnc, $var);
141 return $var;
144 public function __isset($key) {
145 $strpos = strpos($key,'_');
146 if (!is_bool($strpos) && $strpos !== 0) {
147 return isset($this->$key);
149 return false;