1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: |
|
14: |
|
15: | namespace LucidFrame\Core;
|
16: |
|
17: | class View
|
18: | {
|
19: | |
20: | |
21: |
|
22: | private $layout;
|
23: | |
24: | |
25: |
|
26: | private $name;
|
27: | |
28: | |
29: |
|
30: | private $data = array();
|
31: | |
32: | |
33: |
|
34: | private $headStyles = array();
|
35: | |
36: | |
37: |
|
38: | private $headScripts = array();
|
39: |
|
40: | |
41: | |
42: |
|
43: | public function __construct()
|
44: | {
|
45: | $this->layout = _cfg('layoutName');
|
46: | }
|
47: |
|
48: | |
49: | |
50: | |
51: | |
52: | |
53: |
|
54: | public function __set($name, $value)
|
55: | {
|
56: | $this->$name = $value;
|
57: | }
|
58: |
|
59: | |
60: | |
61: | |
62: | |
63: | |
64: |
|
65: | public function __get($name)
|
66: | {
|
67: | return $this->$name;
|
68: | }
|
69: |
|
70: | |
71: | |
72: | |
73: | |
74: | |
75: |
|
76: |
|
77: | public function addData($key, $value)
|
78: | {
|
79: | $this->data[$key] = $value;
|
80: | }
|
81: |
|
82: | |
83: | |
84: | |
85: | |
86: |
|
87: | public function addHeadStyle($file)
|
88: | {
|
89: | $this->headStyles[] = $file;
|
90: | $this->headStyles = array_unique($this->headStyles);
|
91: | }
|
92: |
|
93: | |
94: | |
95: | |
96: | |
97: |
|
98: | public function addHeadScript($file)
|
99: | {
|
100: | $this->headScripts[] = $file;
|
101: | $this->headScripts = array_unique($this->headScripts);
|
102: | }
|
103: |
|
104: | |
105: | |
106: | |
107: | |
108: | |
109: |
|
110: | public function load($name = '')
|
111: | {
|
112: | $name = $name ?: $this->name;
|
113: |
|
114: | if ($name) {
|
115: | $viewName = 'view_' . $name;
|
116: | } else {
|
117: | $viewName = 'view';
|
118: | }
|
119: |
|
120: | $view = _i(_ds(_cr(), $viewName . '.php'));
|
121: | if ($view) {
|
122: | extract($this->data);
|
123: | include $view;
|
124: | } else {
|
125: | throw new \RuntimeException('View file is missing.');
|
126: | }
|
127: | }
|
128: |
|
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: |
|
136: | public function block($name, array $data = array(), $return = false)
|
137: | {
|
138: | $name = rtrim($name, '.php') . '.php';
|
139: |
|
140: | $paths = array();
|
141: | if (strrpos($name, '/') !== false) {
|
142: | $paths[] = $name;
|
143: | } else {
|
144: | $paths[] = _ds(_cr(), $name);
|
145: | }
|
146: |
|
147: | $paths[] = _ds('inc', 'tpl', $name);
|
148: |
|
149: | $this->data = array_merge($this->data, $data);
|
150: |
|
151: | foreach ($paths as $file) {
|
152: | $block = _i($file);
|
153: | if ($block) {
|
154: | extract($this->data);
|
155: | if ($return) {
|
156: | ob_start();
|
157: | include $block;
|
158: | return ob_get_clean();
|
159: | } else {
|
160: | include $block;
|
161: | }
|
162: |
|
163: | return;
|
164: | }
|
165: | }
|
166: |
|
167: | throw new \RuntimeException('Block view file "' . $name . '" is missing.');
|
168: | }
|
169: |
|
170: | |
171: | |
172: |
|
173: | public function headStyle()
|
174: | {
|
175: | foreach ($this->headStyles as $file) {
|
176: | _css($file);
|
177: | }
|
178: | }
|
179: |
|
180: | |
181: | |
182: |
|
183: | public function headScript()
|
184: | {
|
185: | foreach ($this->headScripts as $file) {
|
186: | _js($file);
|
187: | }
|
188: | }
|
189: | }
|
190: | |