Skip to content

Commit

Permalink
[TwigComponent] Add support for namespaced templates in TemplateMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakyvv committed Aug 24, 2023
1 parent 02d661b commit bb1286c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/TwigComponent/src/Twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function compile(Compiler $compiler): void
->raw('), ')
->raw($this->getAttribute('only') ? '[]' : '$context')
->raw(', ')
->string($this->getAttribute('name'))
->string($this->parseTemplateName($this->getAttribute('name')))
->raw(', ')
->raw($this->getAttribute('index'))
->raw(");\n")
Expand All @@ -91,4 +91,20 @@ public function compile(Compiler $compiler): void
->raw("\n")
;
}

/**
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class
*/
private function parseTemplateName(string $name): mixed
{
if (isset($name[0]) && '@' == $name[0]) {
if (false === $pos = strpos($name, '/')) {
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
}

return substr($name, $pos + 1);
}

return $name;
}
}

0 comments on commit bb1286c

Please sign in to comment.