diff --git a/src/main/php/web/frontend/TemplatesFrom.class.php b/src/main/php/web/frontend/TemplatesFrom.class.php new file mode 100755 index 0000000..24214bd --- /dev/null +++ b/src/main/php/web/frontend/TemplatesFrom.class.php @@ -0,0 +1,65 @@ +default= $this->asTemplates($default); + foreach ($namespaced as $ns => $t) { + $this->namespaced[$ns]= $this->asTemplates($t); + } + } + + /** + * Casts given argument to a `Templates` instance. + * + * @param string|io.Path|io.Folder|parent $arg + * @return parent + */ + private function asTemplates($arg) { + return $arg instanceof parent ? $arg : new FilesIn($arg); + } + + /** + * Load a template by a given name + * + * @param string $name The template name, not including the file extension + * @return com.github.mustache.templates.Input + * @throws util.NoSuchElementException + */ + public function source($name) { + if (false === ($p= strpos($name, ':'))) return $this->default->source($name); + + $ns= substr($name, 0, $p); + if ($t= ($this->namespaced[$ns] ?? null)) return $t->source(substr($name, $p + 1)); + + throw new NoSuchElementException('Unknown namespace "'.$ns.'"'); + } + + /** + * Returns available templates + * + * @return com.github.mustache.TemplateListing + */ + public function listing() { + return $this->default->listing(); // FIXME + } +} \ No newline at end of file diff --git a/src/test/php/web/frontend/unittest/TemplatesFromTest.class.php b/src/test/php/web/frontend/unittest/TemplatesFromTest.class.php new file mode 100755 index 0000000..9f47736 --- /dev/null +++ b/src/test/php/web/frontend/unittest/TemplatesFromTest.class.php @@ -0,0 +1,65 @@ +templates); + } + + #[Test] + public function can_create_with_path() { + new TemplatesFrom('src/main/handlebars'); + } + + #[Test, Expect(class: NoSuchElementException::class, message: 'Unknown namespace "nonexistant"')] + public function errors_for_non_existant_namespaces() { + (new TemplatesFrom($this->templates))->source('nonexistant:name'); + } + + #[Test] + public function render_directly() { + $fixture= new Handlebars(new TemplatesFrom( + $this->templates, + ['layout' => (new InMemory())->add('content', '