-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmartyRenderer.php
More file actions
42 lines (33 loc) · 1.07 KB
/
SmartyRenderer.php
File metadata and controls
42 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Schranz\Templating\Adapter\Smarty;
use Schranz\Templating\TemplateRenderer\TemplateRendererInterface;
use Smarty;
class SmartyRenderer implements TemplateRendererInterface
{
/**
* @var Smarty
*/
private $smarty;
public function __construct(Smarty $smarty) {
$this->smarty = $smarty;
}
public function render(string $template, array $context): string
{
foreach ($context as $key => $value) {
$this->smarty->assign($key, $value);
}
// Smarty does not support render to string, so the mechanism from twig is used to render it into a string:
// https://github.com/twigphp/Twig/blob/b6017005b3f6cfc476a976d7cfd57c038f183569/src/Template.php#L370-L389
$level = ob_get_level();
ob_start(function () { return ''; });
try {
$this->smarty->display($template);
} catch (\Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
return ob_get_clean();
}
}