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