Skip to content

Commit bb1286c

Browse files
committed
[TwigComponent] Add support for namespaced templates in TemplateMap
1 parent 02d661b commit bb1286c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/TwigComponent/src/Twig/ComponentNode.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function compile(Compiler $compiler): void
6868
->raw('), ')
6969
->raw($this->getAttribute('only') ? '[]' : '$context')
7070
->raw(', ')
71-
->string($this->getAttribute('name'))
71+
->string($this->parseTemplateName($this->getAttribute('name')))
7272
->raw(', ')
7373
->raw($this->getAttribute('index'))
7474
->raw(");\n")
@@ -91,4 +91,20 @@ public function compile(Compiler $compiler): void
9191
->raw("\n")
9292
;
9393
}
94+
95+
/**
96+
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class
97+
*/
98+
private function parseTemplateName(string $name): mixed
99+
{
100+
if (isset($name[0]) && '@' == $name[0]) {
101+
if (false === $pos = strpos($name, '/')) {
102+
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
103+
}
104+
105+
return substr($name, $pos + 1);
106+
}
107+
108+
return $name;
109+
}
94110
}

0 commit comments

Comments
 (0)