Ubiquity  2.0.2
php rapid development framework
View.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\views;
4 
8 
14 class View {
15  private $vars;
16 
17  public function __construct() {
18  $this->vars=array ();
19  }
20 
21  protected function includeFileAsString($filename) {
22  \ob_start();
23  include ($filename);
24  return \ob_get_clean();
25  }
26 
27  public function setVar($name, $value) {
28  $this->vars[$name]=$value;
29  return $this;
30  }
31 
32  public function setVars($vars) {
33  if (\is_array($vars))
34  $this->vars=\array_merge($this->vars, $vars);
35  else
36  $this->vars=$vars;
37  return $this;
38  }
39 
40  public function getVar($name) {
41  if (\array_key_exists($name, $this->vars)) {
42  return $this->vars[$name];
43  }
44  }
45 
53  public function render($viewName, $asString=false) {
54  $config=Startup::getConfig();
55  $fileName=ROOT . DS . "views/" . $viewName;
56  $ext=\pathinfo($fileName, PATHINFO_EXTENSION);
57  if ($ext === null)
58  $viewName=$viewName . ".php";
59  $fileName=ROOT . DS . "views/" . $viewName;
60  if (\file_exists($fileName)) {
61  $data=$this->vars;
62  if (!UString::endswith($fileName, ".php") && @$config["templateEngine"] instanceof TemplateEngine) {
63  return $config["templateEngine"]->render($viewName, $data, $asString);
64  }
65 
66  if (is_array($data)) {
67  extract($data);
68  }
69  if ($asString) {
70  return $this->includeFileAsString($fileName);
71  } else {
72  include ($fileName);
73  }
74  } else {
75  throw new \Exception("Vue inexistante : " . $viewName);
76  }
77  }
78 }
setVars($vars)
Definition: View.php:32
includeFileAsString($filename)
Definition: View.php:21
render($viewName, $asString=false)
affiche la vue $viewName
Definition: View.php:53
getVar($name)
Definition: View.php:40
setVar($name, $value)
Definition: View.php:27
Représente une vue.
Definition: View.php:14
static endswith($hay, $needle)
Definition: UString.php:16