|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TemplateView.php |
|
5
|
|
|
* |
|
6
|
|
|
* A view wrapper for the template library provided by the jaxon-utils package. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\App\View; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
|
18
|
|
|
|
|
19
|
|
|
class TemplateView implements ViewInterface |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The Jaxon template engine |
|
23
|
|
|
* |
|
24
|
|
|
* @var TemplateEngine |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $xTemplateEngine; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
|
|
|
|
|
29
|
|
|
* The class constructor |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(TemplateEngine $xTemplateEngine) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
|
34
|
|
|
} |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Add a namespace to this view renderer |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $sNamespace The namespace name |
|
|
|
|
|
|
40
|
|
|
* @param string $sDirectory The namespace directory |
|
|
|
|
|
|
41
|
|
|
* @param string $sExtension The extension to append to template names |
|
|
|
|
|
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = '') |
|
46
|
|
|
{ |
|
47
|
|
|
$this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension); |
|
48
|
|
|
} |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Render a view |
|
52
|
|
|
* |
|
53
|
|
|
* @param Store $store A store populated with the view data |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return string The string representation of the view |
|
56
|
|
|
*/ |
|
57
|
|
|
public function render(Store $store): string |
|
58
|
|
|
{ |
|
59
|
|
|
$sViewName = $store->getViewName(); |
|
60
|
|
|
$sNamespace = $store->getNamespace(); |
|
61
|
|
|
// In this view renderer, the namespace must always be prepended to the view name. |
|
62
|
|
|
if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
63
|
|
|
{ |
|
64
|
|
|
$sViewName = $sNamespace . '::' . $sViewName; |
|
65
|
|
|
} |
|
66
|
|
|
// Render the template |
|
67
|
|
|
return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n"); |
|
68
|
|
|
} |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|