Skip to content

Commit

Permalink
correctly load theme templates
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Sep 26, 2024
1 parent ea3e4ae commit 26a5eae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/Filesystem/TwigTemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getTemplateContext(string $templateName, array $options = []): T
$pathLength = \strlen($themeFolder);

foreach ($template['paths'] as $path) {
if ($themeFolder === substr($path, 0, $pathLength)) {
if (str_starts_with($path, '@Contao_Theme_'.$themeFolder)) {
return new TemplateContext($templateName, $path, $template['pathInfo'][$path]);
}
}
Expand All @@ -129,6 +129,16 @@ public function getTemplateContext(string $templateName, array $options = []): T
}
}

$key = array_key_last($template['paths']);
while (isset($template['paths'][$key])) {
if (!str_starts_with($template['paths'][$key], '@Contao_Theme_')) {
return new TemplateContext($templateName, $template['paths'][$key], $template['pathInfo'][$template['paths'][$key]]);
}
$key--;
}

throw new TemplateNotFoundException(sprintf('Unable to find template "%s".', $templateName));

$path = end($template['paths']);

return new TemplateContext($templateName, $path, $template['pathInfo'][$path]);
Expand Down Expand Up @@ -480,18 +490,19 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
continue;
}

$prefix = $namespace;

if (empty($namespace) && str_contains($name, '/')) {
$parts = explode('/', $name);
if (isset($contaoThemePaths[$parts[0]])
&& (Path::getLongestCommonBasePath($contaoThemePaths[$parts[0]], $templatePath)) === $contaoThemePaths[$parts[0]]) {
$namespace = $parts[0];
$prefix = 'Contao_Theme_'.$parts[0];
$name = Path::makeRelative($templatePath, $contaoThemePaths[$parts[0]]);
}
$twigPath = ($namespace ? "$namespace/" : '').$name;
} else {
$twigPath = ($namespace ? "@$namespace/" : '').$name;
}

$twigPath = ($prefix ? "@$prefix/" : '').$name;

if (!$extension) {
if (str_ends_with($name, '.html.twig')) {
$name = substr($name, 0, -10);
Expand Down
15 changes: 14 additions & 1 deletion tests/Filesystem/TwigTemplateLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,22 @@ public function testGetTemplatePathNews()
$scopeMather->method('isFrontendRequest')->willReturn(true);
$parameters['scope_matcher'] = $scopeMather;
$instance = $this->createTestInstance($parameters);

$this->assertSame('ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_App/ce_headline.html.twig', $instance->getTemplatePath('ce_headline', ['disableCache' => true]));
$this->assertSame('@Contao_a/ce_html.html.twig', $instance->getTemplatePath('ce_html', ['disableCache' => true]));

$GLOBALS['objPage'] = (object) ['templateGroup' => 'customtheme'];
$this->assertSame('customtheme/ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_Theme_customtheme/ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_Theme_customtheme/ce_headline.html.twig', $instance->getTemplatePath('ce_headline', ['disableCache' => true]));
$this->assertSame('@Contao_a/ce_html.html.twig', $instance->getTemplatePath('ce_html', ['disableCache' => true]));

$GLOBALS['objPage'] = (object) ['templateGroup' => 'anothertheme'];
$this->assertSame('@Contao_Theme_anothertheme/ce_text.html.twig', $instance->getTemplatePath('ce_text', ['disableCache' => true]));
$this->assertSame('@Contao_App/ce_headline.html.twig', $instance->getTemplatePath('ce_headline', ['disableCache' => true]));
$this->assertSame('@Contao_Theme_anothertheme/ce_html.html.twig', $instance->getTemplatePath('ce_html', ['disableCache' => true]));

unset($GLOBALS['objPage']);
}

private function prepareTemplateLoader(array $parameters): array
Expand Down
Empty file.
Empty file.
Empty file.

0 comments on commit 26a5eae

Please sign in to comment.