1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Utils; |
4
|
|
|
|
5
|
|
|
use Flynt\ComponentManager; |
6
|
|
|
use Twig_Environment; |
7
|
|
|
use Twig_Extension; |
8
|
|
|
use Twig\TwigFunction; |
9
|
|
|
|
10
|
|
|
class TwigExtensionFlynt extends Twig_Extension |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
public function getName() |
13
|
|
|
{ |
14
|
|
|
return 'twig_extension_flynt'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getFunctions() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
new TwigFunction('renderComponent', [$this, 'renderComponent'], array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))), |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function renderComponent(Twig_Environment $env, $context, $componentName, $data = [], $withContext = true, $ignoreMissing = false, $sandboxed = false) |
25
|
|
|
{ |
26
|
|
|
$data = $data === false ? [] : $data; |
27
|
|
|
|
28
|
|
|
if (is_array($componentName)) { |
29
|
|
|
$data = array_merge($componentName, $data); |
30
|
|
|
$componentName = ucfirst($data['acf_fc_layout']); |
31
|
|
|
} |
32
|
|
|
$data = $this->getComponentData($data, $componentName); |
33
|
|
|
|
34
|
|
|
$componentManager = ComponentManager::getInstance(); |
35
|
|
|
$templateFilename = apply_filters('Flynt/TimberLoader/templateFilename', 'index.twig'); |
36
|
|
|
$templateFilename = apply_filters("Flynt/TimberLoader/templateFilename?name=${componentName}", $templateFilename); |
37
|
|
|
$filePath = $componentManager->getComponentFilePath($componentName, $templateFilename); |
38
|
|
|
$relativeFilePath = ltrim(str_replace(get_template_directory(), '', $filePath), '/'); |
39
|
|
|
|
40
|
|
|
if (!is_file($filePath)) { |
41
|
|
|
trigger_error("Template not found: {$filePath}", E_USER_WARNING); |
42
|
|
|
return ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$loader = $env->getLoader(); |
46
|
|
|
|
47
|
|
|
$loaderPaths = $loader->getPaths(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$loader->addPath(dirname($filePath)); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$output = twig_include($env, $context, $relativeFilePath, $data, $withContext, $ignoreMissing, $sandboxed); |
52
|
|
|
|
53
|
|
|
$loader->setPaths($loaderPaths); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$output = apply_filters( |
56
|
|
|
'Flynt/renderComponent', |
57
|
|
|
$output, |
58
|
|
|
$componentName, |
59
|
|
|
$data |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $output; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getComponentData($data, $componentName) |
66
|
|
|
{ |
67
|
|
|
$data = apply_filters( |
68
|
|
|
'Flynt/addComponentData', |
69
|
|
|
$data, |
70
|
|
|
$componentName |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|