Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e9b81d

Browse files
committed
delegate rendering to twig if a template surrogate exists
1 parent 5eda502 commit 5e9b81d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

core-bundle/src/Resources/contao/library/Contao/TemplateInheritance.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Contao\CoreBundle\Monolog\ContaoContext;
1414
use Psr\Log\LogLevel;
15+
use Webmozart\PathUtil\Path;
1516

1617
/**
1718
* Provides the template inheritance logic
@@ -68,13 +69,24 @@ trait TemplateInheritance
6869
*/
6970
protected $intBufferLevel = 0;
7071

72+
/**
73+
* Allow to delegate rendering to Twig
74+
* @var bool
75+
*/
76+
protected $blnEnableTwigSurrogateRendering = true;
77+
7178
/**
7279
* Parse the template file and return it as string
7380
*
7481
* @return string The template markup
7582
*/
7683
public function inherit()
7784
{
85+
if ($this->blnEnableTwigSurrogateRendering && null !== ($result = $this->renderTwigSurrogateIfExists()))
86+
{
87+
return $result;
88+
}
89+
7890
$strBuffer = '';
7991

8092
// Start with the template itself
@@ -323,6 +335,35 @@ protected function getTemplatePath($strTemplate, $strFormat='html5', $blnDefault
323335

324336
return Controller::getTemplate($strTemplate);
325337
}
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+
}
326367
}
327368

328369
class_alias(TemplateInheritance::class, 'TemplateInheritance');

0 commit comments

Comments
 (0)