|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The class is responsible for rendering HTML templates |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category OpCacheGUI |
|
8
|
|
|
* @package Presentation |
|
9
|
|
|
* @author Pieter Hordijk <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa> |
|
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
12
|
|
|
* @version 1.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace OpCacheGUI\Presentation; |
|
15
|
|
|
|
|
16
|
|
|
use OpCacheGUI\I18n\Translator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The class is responsible for rendering HTML templates |
|
20
|
|
|
* |
|
21
|
|
|
* @category OpCacheGUI |
|
22
|
|
|
* @package Presentation |
|
23
|
|
|
* @author Pieter Hordijk <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Html extends Template |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string The base (skeleton) page in which all templates will get rendered |
|
29
|
|
|
*/ |
|
30
|
|
|
private $baseTemplate; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \OpCacheGUI\Presentation\UrlRenderer Instance of an URI renderer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $url; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates instance |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $templateDirectory The directory where all the templates are stored |
|
41
|
|
|
* @param string $baseTemplate The base (skeleton) page in which all templates |
|
42
|
|
|
* will get rendered |
|
43
|
|
|
* @param \OpCacheGUI\I18n\Translator $translator The translation service |
|
44
|
|
|
* @param \OpCacheGUI\Presentation\UrlRenderer $url Instance of an URI renderer |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function __construct($templateDirectory, $baseTemplate, Translator $translator, UrlRenderer $url) |
|
47
|
|
|
{ |
|
48
|
3 |
|
parent::__construct($templateDirectory, $translator); |
|
49
|
|
|
|
|
50
|
3 |
|
$this->baseTemplate = $baseTemplate; |
|
51
|
3 |
|
$this->url = $url; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Renders a template |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $template The template to render |
|
58
|
|
|
* @param array $data The data to use in the template |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function render($template, array $data = []) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->variables = $data; |
|
63
|
|
|
|
|
64
|
1 |
|
$this->variables['content'] = $this->renderTemplate($template); |
|
65
|
|
|
|
|
66
|
1 |
|
return $this->renderTemplate($this->baseTemplate); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Renders the template file using output buffering |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $template The template to render |
|
73
|
|
|
* |
|
74
|
|
|
* @return string The rendered template |
|
75
|
|
|
*/ |
|
76
|
1 |
|
private function renderTemplate($template) |
|
77
|
|
|
{ |
|
78
|
1 |
|
ob_start(); |
|
79
|
1 |
|
require $this->templateDirectory . '/' . $template; |
|
80
|
1 |
|
$content = ob_get_contents(); |
|
81
|
1 |
|
ob_end_clean(); |
|
82
|
|
|
|
|
83
|
1 |
|
return $content; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|