-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwigBlockRenderer.php
More file actions
44 lines (35 loc) · 1.08 KB
/
TwigBlockRenderer.php
File metadata and controls
44 lines (35 loc) · 1.08 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
43
44
<?php
declare(strict_types=1);
namespace Setono\EditorJSBundle\BlockRenderer;
use Setono\EditorJS\Block\Block;
use Setono\EditorJS\BlockRenderer\BlockRendererInterface;
use Setono\EditorJSBundle\Exception\TwigRenderException;
use function Symfony\Component\String\u;
use Twig\Environment;
final class TwigBlockRenderer implements BlockRendererInterface
{
public function __construct(private readonly Environment $twig)
{
}
public function render(Block $block): string
{
$reflectionClass = new \ReflectionClass($block);
$blockName = u($reflectionClass->getShortName())
->snake()
->trimSuffix('_block')
->toString()
;
$template = sprintf('@SetonoEditorJS/block/%s.html.twig', $blockName);
try {
return $this->twig->render($template, [
'block' => $block,
]);
} catch (\Throwable $e) {
throw TwigRenderException::fromThrowable($template, $e);
}
}
public function supports(Block $block): bool
{
return true;
}
}