1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\View; |
4
|
|
|
|
5
|
|
|
#use \Anax\View\View2 as View; |
6
|
|
|
use \Anax\View\View2; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Define helper functions to include before processing the view template. |
10
|
|
|
* The functions here are exposed to the view and can be used in the view. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Shortcut to create an url for a static asset. |
15
|
|
|
* |
16
|
|
|
* @param string $url url to use when creating the url. |
17
|
|
|
* |
18
|
|
|
* @return string as resulting url. |
19
|
|
|
*/ |
20
|
|
|
function asset($url = "") |
21
|
|
|
{ |
22
|
|
|
global $di; |
23
|
|
|
return $di->get("url")->asset($url); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Shortcut to create an url for routing in the framework. |
30
|
|
|
* |
31
|
|
|
* @param null|string $url url to use when creating the url. |
32
|
|
|
* |
33
|
|
|
* @return string as resulting url. |
34
|
|
|
*/ |
35
|
|
|
function url($url = "") |
36
|
|
|
{ |
37
|
|
|
global $di; |
38
|
|
|
return $di->get("url")->create($url); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Render a view with an optional data set of variables. |
45
|
|
|
* |
46
|
|
|
* @param string $template the template file, or array |
47
|
|
|
* @param array $data variables to make available to the |
48
|
|
|
* view, default is empty |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
function renderView($template, $data = []) |
53
|
|
|
{ |
54
|
|
|
global $di; |
55
|
|
|
$view = new View2(); |
56
|
|
|
$template = $di->get("view")->getTemplateFile($template); |
57
|
|
|
$view->set($template, $data); |
58
|
|
|
$view->render($this->app); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if the region in the view container has views to render. |
65
|
|
|
* |
66
|
|
|
* @param string $region to check |
67
|
|
|
* |
68
|
|
|
* @return boolean true or false |
69
|
|
|
*/ |
70
|
|
|
function regionHasContent($region) |
71
|
|
|
{ |
72
|
|
|
global $di; |
73
|
|
|
return $di->get("view")->hasContent($region); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Render views, from the view container, in the region. |
80
|
|
|
* |
81
|
|
|
* @param string $region to render in |
82
|
|
|
* |
83
|
|
|
* @return boolean true or false |
84
|
|
|
*/ |
85
|
|
|
function renderRegion($region) |
86
|
|
|
{ |
87
|
|
|
global $di; |
88
|
|
|
return $di->get("view")->render($region); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a class attribute from a string or array. |
95
|
|
|
* |
96
|
|
|
* @param string|array $args variable amount of classlists. |
97
|
|
|
* |
98
|
|
|
* @return string as complete class attribute |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
function classList(...$args) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$classes = []; |
103
|
|
|
|
104
|
|
|
foreach ($args as $arg) { |
105
|
|
|
if (empty($arg)) { |
106
|
|
|
continue; |
107
|
|
|
} elseif (is_string($arg)) { |
108
|
|
|
$arg = explode(" ", $arg); |
109
|
|
|
} |
110
|
|
|
$classes = array_merge($classes, $arg); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return "class=\"" . implode(" ", $classes) . "\""; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get current url, without querystring. |
120
|
|
|
* |
121
|
|
|
* @return string as resulting url. |
122
|
|
|
*/ |
123
|
|
|
function currentUrl() |
124
|
|
|
{ |
125
|
|
|
global $di; |
126
|
|
|
return $di->get("request")->getCurrentUrl(false); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get current route. |
133
|
|
|
* |
134
|
|
|
* @return string as resulting route. |
135
|
|
|
*/ |
136
|
|
|
function currentRoute() |
137
|
|
|
{ |
138
|
|
|
global $di; |
139
|
|
|
return $di->get("request")->getRoute(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Show variables/functions that are currently defined and can |
146
|
|
|
* be used within the view. Call the function with get_defined_vars() |
147
|
|
|
* as the parameter. |
148
|
|
|
* |
149
|
|
|
* @param array $variables should be the retiurned value from |
150
|
|
|
* get_defined_vars() |
151
|
|
|
* |
152
|
|
|
* @return string showing variables. |
153
|
|
|
*/ |
154
|
|
|
function showEnvironment($variables) |
155
|
|
|
{ |
156
|
|
|
$all = array_keys($variables); |
157
|
|
|
sort($all); |
158
|
|
|
$res = "<pre>\nVariables (var_dump them for more information):\n"; |
159
|
|
|
foreach ($all as $var) { |
160
|
|
|
$variable = $variables[$var]; |
161
|
|
|
$res .= " $var (" . gettype($variable) . ")"; |
162
|
|
|
if (is_integer($variable) || is_double($variable) ) { |
163
|
|
|
$res .= ": $variable"; |
164
|
|
|
} elseif (is_string($variable)) { |
165
|
|
|
$res .= ": \"$variable\""; |
166
|
|
|
} elseif (is_bool($variable)) { |
167
|
|
|
$res .= ": " . ( $variable ? "true" : "false" ); |
168
|
|
|
} |
169
|
|
|
$res .= "\n"; |
170
|
|
|
} |
171
|
|
|
$res .= "</pre>"; |
172
|
|
|
|
173
|
|
|
return $res; |
174
|
|
|
} |
175
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.