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: | $page = _app('page');
|
121: | if ($page instanceof \Closure) {
|
122: | echo $page();
|
123: | } else {
|
124: | $view = _i(_ds(_cr(), $viewName . '.php'));
|
125: | if ($view) {
|
126: | extract($this->data);
|
127: | include $view;
|
128: | } else {
|
129: | throw new \RuntimeException('View file is missing.');
|
130: | }
|
131: | }
|
132: | }
|
133: |
|
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: |
|
141: | public function block($name, array $data = array(), $return = false)
|
142: | {
|
143: | $name = rtrim($name, '.php') . '.php';
|
144: |
|
145: | $paths = array();
|
146: | if (strrpos($name, '/') !== false) {
|
147: | $paths[] = $name;
|
148: | } else {
|
149: | $paths[] = _ds(_cr(), $name);
|
150: | }
|
151: |
|
152: | $paths[] = _ds('inc', 'tpl', $name);
|
153: |
|
154: | $this->data = array_merge($this->data, $data);
|
155: |
|
156: | foreach ($paths as $file) {
|
157: | if (is_file($file) && file_exists($file)) {
|
158: | $block = $file;
|
159: | } else {
|
160: | $block = _i($file);
|
161: | }
|
162: |
|
163: | if ($block) {
|
164: | extract($this->data);
|
165: | if ($return) {
|
166: | ob_start();
|
167: | include $block;
|
168: | return ob_get_clean();
|
169: | } else {
|
170: | include $block;
|
171: | }
|
172: |
|
173: | return;
|
174: | }
|
175: | }
|
176: |
|
177: | throw new \RuntimeException('Block view file "' . $name . '" is missing.');
|
178: | }
|
179: |
|
180: | |
181: | |
182: |
|
183: | public function headStyle()
|
184: | {
|
185: | foreach ($this->headStyles as $file) {
|
186: | _css($file);
|
187: | }
|
188: | }
|
189: |
|
190: | |
191: | |
192: |
|
193: | public function headScript()
|
194: | {
|
195: | foreach ($this->headScripts as $file) {
|
196: | _js($file);
|
197: | }
|
198: | }
|
199: | }
|
200: | |