|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework; |
|
3
|
|
|
|
|
4
|
|
|
class AbstractOutputLoader |
|
5
|
|
|
{ |
|
6
|
|
|
protected $projectPath; |
|
7
|
|
|
protected $htmlOutput; |
|
8
|
|
|
protected $jsonOutput; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct($projectPath, $htmlOutput = null, $jsonOutput = null) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->projectPath = $projectPath; |
|
13
|
|
|
$this->htmlOutput = $htmlOutput; |
|
14
|
|
|
$this->jsonOutput = $jsonOutput; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
protected function htmlOutput() |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->htmlOutput; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function jsonOutput() |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->jsonOutput; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function getRenderedHtml($template) |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Set template path. |
|
31
|
|
|
*/ |
|
32
|
|
|
$this->htmlOutput()->setPath("{$this->projectPath}resources/views/"); |
|
33
|
|
|
/** |
|
34
|
|
|
* Set page template |
|
35
|
|
|
*/ |
|
36
|
|
|
$this->htmlOutput()->setTemplate($template); |
|
37
|
|
|
return $this->htmlOutput()->render(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function setHtmlTemplateData($data) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!is_array($data)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
foreach ($data as $key => $value) { |
|
46
|
|
|
$this->htmlOutput()->setData($key, $value); |
|
47
|
|
|
} |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function html($data, $template) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->setHtmlTemplateData($data); |
|
54
|
|
|
return $this->getRenderedHtml($template); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function htmlPage($data, $pageTemplate, $mainTemplate = null) |
|
58
|
|
|
{ |
|
59
|
|
|
/** |
|
60
|
|
|
* Set template data. |
|
61
|
|
|
*/ |
|
62
|
|
|
$this->setHtmlTemplateData($data); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Partials |
|
66
|
|
|
*/ |
|
67
|
|
|
foreach ($this->htmlOutput()->setting('partials', []) as $partialTemplate) { |
|
68
|
|
|
$this->htmlOutput()->setData( |
|
69
|
|
|
"tpl_{$partialTemplate}", |
|
70
|
|
|
$this->getRenderedHtml($partialTemplate) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
/** |
|
74
|
|
|
* Page content |
|
75
|
|
|
*/ |
|
76
|
|
|
$this->htmlOutput()->setData( |
|
77
|
|
|
'tpl_content', |
|
78
|
|
|
$this->getRenderedHtml($pageTemplate) |
|
79
|
|
|
); |
|
80
|
|
|
/** |
|
81
|
|
|
* Main template |
|
82
|
|
|
*/ |
|
83
|
|
|
$mainTemplate = $mainTemplate ? $mainTemplate : |
|
84
|
|
|
$this->htmlOutput()->setting('main_template', 'layout'); |
|
85
|
|
|
return $this->getRenderedHtml($mainTemplate); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function json($data) |
|
89
|
|
|
{ |
|
90
|
|
|
if (is_array($data)) { |
|
91
|
|
|
foreach ($data as $key => $value) { |
|
92
|
|
|
$this->jsonOutput()->setData($key, $value); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
return $this->jsonOutput()->render(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|