|
12 | 12 |
|
13 | 13 | use Contao\CoreBundle\Monolog\ContaoContext;
|
14 | 14 | use Psr\Log\LogLevel;
|
| 15 | +use Webmozart\PathUtil\Path; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * Provides the template inheritance logic
|
@@ -68,13 +69,24 @@ trait TemplateInheritance
|
68 | 69 | */
|
69 | 70 | protected $intBufferLevel = 0;
|
70 | 71 |
|
| 72 | + /** |
| 73 | + * Allow to delegate rendering to Twig |
| 74 | + * @var bool |
| 75 | + */ |
| 76 | + protected $blnEnableTwigSurrogateRendering = true; |
| 77 | + |
71 | 78 | /**
|
72 | 79 | * Parse the template file and return it as string
|
73 | 80 | *
|
74 | 81 | * @return string The template markup
|
75 | 82 | */
|
76 | 83 | public function inherit()
|
77 | 84 | {
|
| 85 | + if ($this->blnEnableTwigSurrogateRendering && null !== ($result = $this->renderTwigSurrogateIfExists())) |
| 86 | + { |
| 87 | + return $result; |
| 88 | + } |
| 89 | + |
78 | 90 | $strBuffer = '';
|
79 | 91 |
|
80 | 92 | // Start with the template itself
|
@@ -323,6 +335,35 @@ protected function getTemplatePath($strTemplate, $strFormat='html5', $blnDefault
|
323 | 335 |
|
324 | 336 | return Controller::getTemplate($strTemplate);
|
325 | 337 | }
|
| 338 | + |
| 339 | + private function renderTwigSurrogateIfExists(): ?string |
| 340 | + { |
| 341 | + $templateCandidates = array( |
| 342 | + "@ContaoLegacy/{$this->strTemplate}.html.twig", |
| 343 | + ); |
| 344 | + |
| 345 | + if (null !== ($page = $GLOBALS['objPage'] ?? null) && null !== $themePath = $page->templateGroup) |
| 346 | + { |
| 347 | + $theme = Path::makeRelative($themePath, 'templates'); |
| 348 | + |
| 349 | + array_unshift( |
| 350 | + $templateCandidates, |
| 351 | + "@ContaoLegacy_$theme/{$this->strTemplate}.html.twig", |
| 352 | + ); |
| 353 | + } |
| 354 | + |
| 355 | + $twig = System::getContainer()->get('twig'); |
| 356 | + |
| 357 | + foreach ($templateCandidates as $templateCandidate) |
| 358 | + { |
| 359 | + if ($twig->getLoader()->exists($templateCandidate)) |
| 360 | + { |
| 361 | + return $twig->render($templateCandidate, $this->arrData); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + return null; |
| 366 | + } |
326 | 367 | }
|
327 | 368 |
|
328 | 369 | class_alias(TemplateInheritance::class, 'TemplateInheritance');
|
0 commit comments