Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template namespaces #16

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/php/web/frontend/TemplatesFrom.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php namespace web\frontend;

use com\github\mustache\templates\Templates;
use com\handlebarsjs\FilesIn;
use util\NoSuchElementException;

/**
* Loads templates from a given path by default, supporting namespaced
* templates (`namespace:name`) from a given map of namespaces and paths
* or loaders.
*
* @test web.frontend.unittest.TemplatesFromTest
*/
class TemplatesFrom extends Templates {
private $default;
private $namespaced= [];

/**
* Creates a new instance
*
* @param string|io.Path|io.Folder|parent $default
* @param [:string|io.Path|io.Folder|parent] $namespaced
*/
public function __construct($default, array $namespaced= []) {
$this->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
}
}
65 changes: 65 additions & 0 deletions src/test/php/web/frontend/unittest/TemplatesFromTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php namespace web\frontend\unittest;

use com\github\mustache\InMemory;
use test\{Assert, Expect, Test, Values};
use util\NoSuchElementException;
use web\frontend\{Handlebars, TemplatesFrom};

class TemplatesFromTest extends HandlebarsTest {

#[Test]
public function can_create_with_templates() {
new TemplatesFrom($this->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', '<h1>{{title}}</h1>')]
));

Assert::equals('<h1>Test</h1>', $fixture->render('layout:content', ['title' => 'Test']));
}

#[Test]
public function referenced_as_partial() {
$fixture= new Handlebars(new TemplatesFrom(
$this->templates->add('fixture', '{{> layout:content}}'),
['layout' => (new InMemory())->add('content', '<h1>{{title}}</h1>')]
));

Assert::equals('<h1>Test</h1>', $fixture->render('fixture', ['title' => 'Test']));
}

#[Test]
public function passing_variables_to_partial() {
$fixture= new Handlebars(new TemplatesFrom(
$this->templates->add('fixture', '{{> layout:content title="Test"}}'),
['layout' => (new InMemory())->add('content', '<h1>{{title}}</h1>')]
));

Assert::equals('<h1>Test</h1>', $fixture->render('fixture', []));
}

#[Test, Values(['layout', 'example/layout-lib'])]
public function partial_blocks($ns) {
$template= "{{#> {$ns}:content}}{{#*inline 'title'}}Test{{/inline}}{{/{$ns}:content}}";
$fixture= new Handlebars(new TemplatesFrom(
$this->templates->add('fixture', $template),
[$ns => (new InMemory())->add('content', '<h1>{{> title}}</h1>')]
));

Assert::equals('<h1>Test</h1>', $fixture->render('fixture', []));
}
}
Loading