ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
View.php
Go to the documentation of this file.
1 <?php
46 class Slim_View {
47 
51  protected $data = array();
52 
57 
63  public function __construct() {}
64 
72  public function getData( $key = null ) {
73  if ( !is_null($key) ) {
74  return isset($this->data[$key]) ? $this->data[$key] : null;
75  } else {
76  return $this->data;
77  }
78  }
79 
97  public function setData() {
98  $args = func_get_args();
99  if ( count($args) === 1 && is_array($args[0]) ) {
100  $this->data = $args[0];
101  } else if ( count($args) === 2 ) {
102  $this->data[(string)$args[0]] = $args[1];
103  } else {
104  throw new InvalidArgumentException('Cannot set View data with provided arguments. Usage: `View::setData( $key, $value );` or `View::setData([ key => value, ... ]);`');
105  }
106  }
107 
113  public function appendData( array $data ) {
114  $this->data = array_merge($this->data, $data);
115  }
116 
121  public function getTemplatesDirectory() {
123  }
124 
131  public function setTemplatesDirectory( $dir ) {
132  $this->templatesDirectory = rtrim($dir, '/');
133  }
134 
143  public function display( $template ) {
144  echo $this->render($template);
145  }
146 
153  public function render( $template ) {
154  extract($this->data);
155  $templatePath = $this->getTemplatesDirectory() . '/' . ltrim($template, '/');
156  if ( !file_exists($templatePath) ) {
157  throw new RuntimeException('View cannot render template `' . $templatePath . '`. Template does not exist.');
158  }
159  ob_start();
160  require $templatePath;
161  return ob_get_clean();
162  }
163 
164 }