|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\View; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\View\View2 as View; |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Define helper functions to include before processing the view template. |
|
9
|
|
|
* The functions here are exposed to the view and can be used in the view. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Shortcut to create an url for a static asset. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $url url to use when creating the url. |
|
16
|
|
|
* |
|
17
|
|
|
* @return string as resulting url. |
|
18
|
|
|
*/ |
|
19
|
|
|
function asset($url = "") |
|
20
|
|
|
{ |
|
21
|
|
|
global $di; |
|
22
|
|
|
return $di->get("url")->asset($url); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Shortcut to create an url for routing in the framework. |
|
29
|
|
|
* |
|
30
|
|
|
* @param null|string $url url to use when creating the url. |
|
31
|
|
|
* |
|
32
|
|
|
* @return string as resulting url. |
|
33
|
|
|
*/ |
|
34
|
|
|
function url($url = "") |
|
35
|
|
|
{ |
|
36
|
|
|
global $di; |
|
37
|
|
|
return $di->get("url")->create($url); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Render a view with an optional data set of variables. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $template the template file, or array |
|
46
|
|
|
* @param array $data variables to make available to the |
|
47
|
|
|
* view, default is empty |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
function renderView($template, $data = []) |
|
52
|
|
|
{ |
|
53
|
|
|
global $di; |
|
54
|
|
|
$view = new View(); |
|
55
|
|
|
$template = $di->get("view")->getTemplateFile($template); |
|
56
|
|
|
$view->set($template, $data); |
|
57
|
|
|
$view->render($this->app); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check if the region in the view container has views to render. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $region to check |
|
66
|
|
|
* |
|
67
|
|
|
* @return boolean true or false |
|
68
|
|
|
*/ |
|
69
|
|
|
function regionHasContent($region) |
|
70
|
|
|
{ |
|
71
|
|
|
global $di; |
|
72
|
|
|
return $di->get("view")->hasContent($region); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Render views, from the view container, in the region. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $region to render in |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean true or false |
|
83
|
|
|
*/ |
|
84
|
|
|
function renderRegion($region) |
|
85
|
|
|
{ |
|
86
|
|
|
global $di; |
|
87
|
|
|
return $di->get("view")->render($region); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create a class attribute from a string or array. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string|array $args variable amount of classlists. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string as complete class attribute |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
function classList(...$args) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$classes = []; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($args as $arg) { |
|
104
|
|
|
if (empty($arg)) { |
|
105
|
|
|
continue; |
|
106
|
|
|
} elseif (is_string($arg)) { |
|
107
|
|
|
$arg = explode(" ", $arg); |
|
108
|
|
|
} |
|
109
|
|
|
$classes = array_merge($classes, $arg); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return "class=\"" . implode(" ", $classes) . "\""; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get current url, without querystring. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string as resulting url. |
|
121
|
|
|
*/ |
|
122
|
|
|
function currentUrl() |
|
123
|
|
|
{ |
|
124
|
|
|
global $di; |
|
125
|
|
|
return $di->get("request")->getCurrentUrl(false); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get current route. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string as resulting route. |
|
134
|
|
|
*/ |
|
135
|
|
|
function currentRoute() |
|
136
|
|
|
{ |
|
137
|
|
|
global $di; |
|
138
|
|
|
return $di->get("request")->getRoute(); |
|
139
|
|
|
} |
|
140
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: