diff --git a/src/main/php/com/handlebarsjs/HandlebarsEngine.class.php b/src/main/php/com/handlebarsjs/HandlebarsEngine.class.php index 9b8220a..f513f4b 100755 --- a/src/main/php/com/handlebarsjs/HandlebarsEngine.class.php +++ b/src/main/php/com/handlebarsjs/HandlebarsEngine.class.php @@ -1,6 +1,7 @@ templates->delegate($l); + public function withTemplates($arg) { + if ($arg instanceof Folder || $arg instanceof Path || is_string($arg)) { + $this->templates->delegate(new FilesIn($arg, ['.handlebars'])); + } else if (is_array($arg)) { + $this->templates->delegate(new InMemory($arg)); + } else { + $this->templates->delegate($arg); + } return $this; } diff --git a/src/test/php/com/handlebarsjs/unittest/EngineTest.class.php b/src/test/php/com/handlebarsjs/unittest/EngineTest.class.php index 72d996c..35ecb52 100755 --- a/src/test/php/com/handlebarsjs/unittest/EngineTest.class.php +++ b/src/test/php/com/handlebarsjs/unittest/EngineTest.class.php @@ -1,13 +1,23 @@ getURI(); }, 'string']; + yield [function($temp) { return new Folder($temp->getURI()); }, 'io.Folder']; + yield [function($temp) { return new Path($temp->getURI()); }, 'io.Path']; + yield [function($temp) { return new FilesIn($temp, ['.handlebars']); }, 'com.github.mustache.FilesIn']; + } + #[Test] public function can_create() { new HandlebarsEngine(); @@ -67,4 +77,38 @@ public function version() { return '1.0.0'; } }); Assert::equals('1.0.0', $engine->parser()->version()); } + + #[Test] + public function templates_initially_empty() { + $engine= new HandlebarsEngine(); + Assert::equals([], $engine->templates()->listing()->templates()); + } + + #[Test] + public function templates_from_loader() { + $engine= (new HandlebarsEngine())->withTemplates(new InMemory(['test' => 'Hello {{name}}'])); + Assert::equals(['test'], $engine->templates()->listing()->templates()); + } + + #[Test] + public function templates_from_map() { + $engine= (new HandlebarsEngine())->withTemplates(['test' => 'Hello {{name}}']); + Assert::equals(['test'], $engine->templates()->listing()->templates()); + } + + #[Test, Values(from: 'files')] + public function templates_from_files($arg) { + $temp= new Folder(Environment::tempDir(), md5(self::class)); + $temp->create(); + + try { + Files::write(new File($temp, 'test.handlebars'), 'Hello {{name}}'); + Files::write(new File($temp, 'translations.csv'), 'Not included in listing'); + + $engine= (new HandlebarsEngine())->withTemplates($arg($temp)); + Assert::equals(['test'], $engine->templates()->listing()->templates()); + } finally { + $temp->unlink(); + } + } } \ No newline at end of file