Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
View.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\views;
4
7
17class View {
18 private $vars;
19
20 public function __construct() {
21 $this->vars = [];
22 }
23
24 protected function includeFileAsString($filename) {
25 \ob_start ();
26 include ($filename);
27 return \ob_get_clean ();
28 }
29
30 public function setVar($name, $value) {
31 $this->vars [$name] = $value;
32 return $this;
33 }
34
35 public function setVars($vars) {
36 if (\is_array ( $vars )){
37 $this->vars = \array_merge ( $this->vars, $vars );
38 } else {
39 $this->vars = $vars;
40 }
41 return $this;
42 }
43
44 public function getVar($name) {
45 return $this->vars [$name]??null;
46 }
47
56 public function render($viewName, $asString = false) {
57 $templateEngine = Startup::$templateEngine;
58 $ext = \pathinfo ( $viewName, PATHINFO_EXTENSION );
59 if ($ext == null){
60 $viewName = $viewName . '.'.Startup::getViewNameFileExtension();
61 }
62 $data = $this->vars;
63 if ($templateEngine instanceof TemplateEngine) {
64 return $templateEngine->render ( $viewName, $data, $asString );
65 }
66
67 if (\is_array ( $data )) {
68 \extract ( $data );
69 }
70 $fileName = \ROOT . \DS . 'views' . \DS . $viewName;
71 if (\file_exists ( $fileName )) {
72 if ($asString) {
73 return $this->includeFileAsString ( $fileName );
74 } else {
75 include ($fileName);
76 }
77 } else {
78 throw new \Exception ( "View {$viewName} not found!" );
79 }
80 }
81
82 public function getBlockNames($templateName) {
83 return Startup::$templateEngine->getBlockNames ( $templateName );
84 }
85
86 public function getCode($templateName) {
87 return Startup::$templateEngine->getCode ( $templateName );
88 }
89}
Starts the framework.
Definition Startup.php:19
Represents a view.
Definition View.php:17
render($viewName, $asString=false)
Renders the view $viewName.
Definition View.php:56
setVar($name, $value)
Definition View.php:30
getBlockNames($templateName)
Definition View.php:82
includeFileAsString($filename)
Definition View.php:24
getCode($templateName)
Definition View.php:86