|
2 | 2 |
|
3 | 3 | namespace Ubiquity\views; |
4 | 4 |
|
5 | | -use Ubiquity\utils\base\UString; |
6 | 5 | use Ubiquity\views\engine\TemplateEngine; |
7 | 6 | use Ubiquity\controllers\Startup; |
8 | 7 |
|
9 | 8 | /** |
10 | | - * Represents a view |
| 9 | + * Represents a view. |
| 10 | + * Ubiquity\views$View |
| 11 | + * This class is part of Ubiquity |
| 12 | + * |
11 | 13 | * @author jcheron <myaddressmail@gmail.com> |
12 | | - * @version 1.0.2 |
| 14 | + * @version 1.0.3 |
13 | 15 | * |
14 | 16 | */ |
15 | 17 | class View { |
16 | 18 | private $vars; |
17 | 19 |
|
18 | 20 | public function __construct() { |
19 | | - $this->vars=array (); |
| 21 | + $this->vars = array (); |
20 | 22 | } |
21 | 23 |
|
22 | 24 | protected function includeFileAsString($filename) { |
23 | | - \ob_start(); |
| 25 | + \ob_start (); |
24 | 26 | include ($filename); |
25 | | - return \ob_get_clean(); |
| 27 | + return \ob_get_clean (); |
26 | 28 | } |
27 | 29 |
|
28 | 30 | public function setVar($name, $value) { |
29 | | - $this->vars[$name]=$value; |
| 31 | + $this->vars [$name] = $value; |
30 | 32 | return $this; |
31 | 33 | } |
32 | 34 |
|
33 | 35 | public function setVars($vars) { |
34 | | - if (\is_array($vars)) |
35 | | - $this->vars=\array_merge($this->vars, $vars); |
| 36 | + if (\is_array ( $vars )) |
| 37 | + $this->vars = \array_merge ( $this->vars, $vars ); |
36 | 38 | else |
37 | | - $this->vars=$vars; |
| 39 | + $this->vars = $vars; |
38 | 40 | return $this; |
39 | 41 | } |
40 | 42 |
|
41 | 43 | public function getVar($name) { |
42 | | - if (\array_key_exists($name, $this->vars)) { |
43 | | - return $this->vars[$name]; |
| 44 | + if (\array_key_exists ( $name, $this->vars )) { |
| 45 | + return $this->vars [$name]; |
44 | 46 | } |
45 | 47 | } |
46 | 48 |
|
47 | 49 | /** |
48 | 50 | * affiche la vue $viewName |
| 51 | + * |
49 | 52 | * @param string $viewName nom de la vue à charger |
50 | 53 | * @param boolean $asString Si vrai, la vue n'est pas affichée mais retournée sous forme de chaîne (utilisable dans une variable) |
51 | 54 | * @throws \Exception |
52 | 55 | * @return string |
53 | 56 | */ |
54 | | - public function render($viewName, $asString=false) { |
55 | | - $templateEngine=Startup::$templateEngine; |
56 | | - $ext=\pathinfo($viewName, PATHINFO_EXTENSION); |
| 57 | + public function render($viewName, $asString = false) { |
| 58 | + $templateEngine = Startup::$templateEngine; |
| 59 | + $ext = \pathinfo ( $viewName, PATHINFO_EXTENSION ); |
57 | 60 | if ($ext === null) |
58 | | - $viewName=$viewName . ".php"; |
59 | | - $data=$this->vars; |
60 | | - if (!UString::endswith($viewName, ".php") && $templateEngine instanceof TemplateEngine) { |
61 | | - return $templateEngine->render($viewName, $data, $asString); |
| 61 | + $viewName = $viewName . '.php'; |
| 62 | + $data = $this->vars; |
| 63 | + if (\substr ( $viewName, - strlen ( '.php' ) ) !== '.php' && $templateEngine instanceof TemplateEngine) { |
| 64 | + return $templateEngine->render ( $viewName, $data, $asString ); |
62 | 65 | } |
63 | 66 |
|
64 | | - if (is_array($data)) { |
65 | | - extract($data); |
| 67 | + if (\is_array ( $data )) { |
| 68 | + \extract ( $data ); |
66 | 69 | } |
67 | | - $fileName=\ROOT . \DS . "views".\DS . $viewName; |
68 | | - if(file_exists($fileName)){ |
| 70 | + $fileName = \ROOT . \DS . 'views' . \DS . $viewName; |
| 71 | + if (\file_exists ( $fileName )) { |
69 | 72 | if ($asString) { |
70 | | - return $this->includeFileAsString($fileName); |
| 73 | + return $this->includeFileAsString ( $fileName ); |
71 | 74 | } else { |
72 | 75 | include ($fileName); |
73 | 76 | } |
74 | | - }else{ |
75 | | - throw new \Exception("View {$viewName} not found!"); |
| 77 | + } else { |
| 78 | + throw new \Exception ( "View {$viewName} not found!" ); |
76 | 79 | } |
77 | 80 | } |
78 | | - |
79 | | - public function getBlockNames($templateName){ |
80 | | - return Startup::$templateEngine->getBlockNames($templateName); |
| 81 | + |
| 82 | + public function getBlockNames($templateName) { |
| 83 | + return Startup::$templateEngine->getBlockNames ( $templateName ); |
81 | 84 | } |
82 | | - |
83 | | - public function getCode($templateName){ |
84 | | - return Startup::$templateEngine->getCode($templateName); |
| 85 | + |
| 86 | + public function getCode($templateName) { |
| 87 | + return Startup::$templateEngine->getCode ( $templateName ); |
85 | 88 | } |
86 | 89 | } |
0 commit comments