Ubiquity  2.0.3
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  $ext=\pathinfo($viewName, PATHINFO_EXTENSION);
56  if ($ext === null)
57  $viewName=$viewName . ".php";
58  $data=$this->vars;
59  if (!UString::endswith($viewName, ".php") && @$config["templateEngine"] instanceof TemplateEngine) {
60  return $config["templateEngine"]->render($viewName, $data, $asString);
61  }
62 
63  if (is_array($data)) {
64  extract($data);
65  }
66  $fileName=ROOT . DS . "views/" . $viewName;
67  if(file_exists($fileName)){
68  if ($asString) {
69  return $this->includeFileAsString($fileName);
70  } else {
71  include ($fileName);
72  }
73  }else{
74  throw new \Exception("View {$viewName} not found!");
75  }
76  }
77 
78  public function getBlockNames($templateName){
79  $config=Startup::getConfig();
80  return $config["templateEngine"]->getBlockNames($templateName);
81  }
82 
83  public function getCode($templateName){
84  $config=Startup::getConfig();
85  return $config["templateEngine"]->getCode($templateName);
86  }
87 }
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
getBlockNames($templateName)
Definition: View.php:78
setVar($name, $value)
Definition: View.php:27
Represents a view.
Definition: View.php:14
getCode($templateName)
Definition: View.php:83
static endswith($hay, $needle)
Definition: UString.php:16