last changes
[ayans.git] / includes / templates.php
bloba7cd9c46f689234804d9a5e3700cd104f6b5337a
1 <?php
3 class templates_exception extends Exception {}
5 class templates {
6 /**
7 * File list
9 * @var array
11 protected $_files = array();
13 /**
14 * Path to the templates files
16 * @var string
18 protected $_templatePath;
20 private $_escape = array('htmlentities');
22 public function __construct($template_path='./templates/') {
23 $this->_templatePath = $template_path;
26 public function getTemplatePath() {
27 return $this->_templatePath;
30 public function addFile($tag,$name) {
31 $this->_files[$tag] = $name;
34 public function render($tag) {
35 try {
36 if (!is_array($tag)) {
37 ob_start();
38 if (isset($this->_files['_begin'])) {
39 include $this->_file('_begin');
41 include $this->_file($tag);
42 if (isset($this->_files['_end'])) {
43 include $this->_file('_end');
45 //return ob_end_clean();
46 } else {
47 ob_start();
48 if (isset($this->_files['_begin'])) {
49 include $this->_file('_begin');
51 $tags = $tag;
52 unset($tag);
53 foreach ($tags as $tag) {
54 include $this->_file($tag);
56 if (isset($this->_files['_end'])) {
57 include $this->_file('_end');
60 ob_end_flush();
61 } catch (templates_exception $e) {
62 die ($e->getMessage());
63 } catch (Exception $e) { die('ex error: '.$e->getMessage()); }
66 private function _file($tag) {
67 if (is_readable($this->_templatePath.$this->_files[$tag])) {
68 return $this->_templatePath.$this->_files[$tag];
69 } else {
70 throw new templates_exception('The file <em>'.$this->_templatePath.$this->_files[$tag]. '</em> is not readable');
74 public function setEscape($ref) {
75 if (!in_array($ref,$this->_escape)) {
76 $this->_escape[] = $ref;
80 public function remEscape($ref,$id=false) {
81 if ($id && isset($this->_escape[$id])) {
82 unset($this->_escape[$id]);
83 return true;
84 } elseif (!$id) {
85 foreach ($this->_escape as $key => $val) {
86 if ($val == $ref) {
87 unset($this->_escape[$key]);
88 return true;
92 return false;
95 public function escape($var) {
96 foreach($this->_escape as $fnct) {
97 if (in_array($fnct, array('htmlentities','htmlspecialchars'))) {
98 $var = call_user_func($fnct, $var, ENT_COMPAT, 'utf8');
99 } else {
100 $var = call_user_func($fnc, $var);
103 return $var;
106 public function __isset($key) {
107 $strpos = strpos($key,'_');
108 if (!is_bool($strpos) && $strpos !== 0) {
109 return isset($this->$key);
111 return false;