-
Notifications
You must be signed in to change notification settings - Fork 0
Partial blocks and inline partials #9
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
Changes from 10 commits
890f1c6
5d9d96b
db97d3d
413ca52
bf1726b
46df23d
9cff7f3
0c4c560
034b6af
d41ff92
9f6a63d
377aba7
f11dd79
b662f32
6e88762
7e7f438
1ef5167
8d23ec6
29517dd
37ea0db
5310886
3136259
27a7539
5e43601
16a0ed6
da39668
e292e24
fd5efef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php namespace com\handlebarsjs; | ||
|
|
||
| use com\github\mustache\NodeList; | ||
|
|
||
| /** | ||
| * Partial blocks | ||
| * | ||
| * @see http://handlebarsjs.com/partials.html | ||
| * @test xp://com.handlebarsjs.unittest.InlinePartialHelperTest | ||
| */ | ||
| class InlinePartialHelper extends BlockNode { | ||
|
|
||
| /** | ||
| * Creates a new with block helper | ||
| * | ||
| * @param string[] $options | ||
| * @param com.github.mustache.NodeList $fn | ||
| * @param com.github.mustache.NodeList $inverse | ||
| * @param string $start | ||
| * @param string $end | ||
| */ | ||
| public function __construct($options= [], NodeList $fn= null, NodeList $inverse= null, $start= '{{', $end= '}}') { | ||
| parent::__construct('inline', $options, $fn, $inverse, $start, $end); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates this node | ||
| * | ||
| * @param com.github.mustache.Context $context the rendering context | ||
| * @return string | ||
| */ | ||
| public function evaluate($context) { | ||
| $f= $this->options[0]; | ||
| $target= $f($this, $context, []); | ||
|
|
||
| $context->engine->getTemplates()->register($target, $this->fn); | ||
| return ''; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php namespace com\handlebarsjs; | ||
|
|
||
| use com\github\mustache\NodeList; | ||
|
|
||
| /** | ||
| * Partial blocks | ||
| * | ||
| * @see http://handlebarsjs.com/partials.html | ||
| * @test xp://com.handlebarsjs.unittest.PartialBlockHelperTest | ||
| */ | ||
| class PartialBlockHelper extends BlockNode { | ||
|
|
||
| /** | ||
| * Creates a new with block helper | ||
| * | ||
| * @param string[] $options | ||
| * @param com.github.mustache.NodeList $fn | ||
| * @param com.github.mustache.NodeList $inverse | ||
| * @param string $start | ||
| * @param string $end | ||
| */ | ||
| public function __construct($options= [], NodeList $fn= null, NodeList $inverse= null, $start= '{{', $end= '}}') { | ||
| $template= (string)array_shift($options); | ||
| parent::__construct($template, $options, $fn, $inverse, $start, $end); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates this node | ||
| * | ||
| * @param com.github.mustache.Context $context the rendering context | ||
| * @return string | ||
| */ | ||
| public function evaluate($context) { | ||
| $templates= $context->engine->getTemplates(); | ||
|
|
||
| $source= $templates->source($this->name); | ||
| if ($source->exists()) { | ||
| $previous= $templates->register('@partial-block', $this->fn); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will re-run the decorators unnecessarily... |
||
| try { | ||
| return $context->engine->render($source, $context, $this->start, $this->end, ''); | ||
| } finally { | ||
| $templates->register('@partial-block', $previous); | ||
| } | ||
| } else { | ||
| return $this->fn->evaluate($context); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php namespace com\handlebarsjs; | ||
|
|
||
| use text\StringTokenizer; | ||
| use com\github\mustache\Node; | ||
| use com\github\mustache\Template; | ||
| use com\github\mustache\templates\Tokens; | ||
| use com\github\mustache\templates\Compiled; | ||
| use com\github\mustache\templates\NotFound; | ||
|
|
||
| /** | ||
| * Template loading implementation | ||
| * | ||
| * @test xp://com.handlebarsjs.unittest.TemplatesTest | ||
| */ | ||
| class Templates extends \com\github\mustache\templates\Templates { | ||
| private $templates= []; | ||
| private $delegate; | ||
|
|
||
| /** | ||
| * Sets delegate loader | ||
| * | ||
| * @param com.github.mustache.templates.Templates $delegate | ||
| * @return void | ||
| */ | ||
| public function delegate($delegate) { | ||
| $this->delegate= $delegate; | ||
| } | ||
|
|
||
| /** | ||
| * Adds template | ||
| * | ||
| * @param string $name | ||
| * @param string|com.github.mustache.Node $content | ||
| * @return string | ||
| */ | ||
| public function register($name, $content) { | ||
| $previous= isset($this->templates[$name]) ? $this->templates[$name] : null; | ||
| if ($content instanceof Node) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch is not tested
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Done! |
||
| $this->templates[$name]= new Compiled(new Template($name, $content)); | ||
| } else { | ||
| $this->templates[$name]= new Tokens($name, new StringTokenizer($content)); | ||
| } | ||
| return $previous; | ||
| } | ||
|
|
||
| /** | ||
| * Load a template by a given name | ||
| * | ||
| * @param string $name The template name without file extension | ||
| * @return com.github.mustache.templates.Source | ||
| */ | ||
| public function source($name) { | ||
| if (isset($this->templates[$name])) { | ||
| return $this->templates[$name]; | ||
| } else if ($this->delegate) { | ||
| return $this->delegate->source($name); | ||
| } else { | ||
| return new NotFound('Cannot find template '.$name); | ||
| } | ||
| } | ||
|
|
||
| /** @return com.github.mustache.TemplateListing */ | ||
| public function listing() { | ||
| return $this->delegate->listing(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct, the listing is missing the templates registered in |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php namespace com\handlebarsjs\unittest; | ||
|
|
||
| class InlinePartialHelperTest extends HelperTest { | ||
|
|
||
| #[@test] | ||
| public function basic_usage() { | ||
| $template= '{{#*inline "myPartial"}}My Content{{/inline}}{{#each children}}{{> myPartial}} {{/each}}'; | ||
| $this->assertEquals('My Content My Content My Content ', $this->evaluate($template, ['children' => [1, 2, 3]])); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php namespace com\handlebarsjs\unittest; | ||
|
|
||
| class PartialBlockHelperTest extends HelperTest { | ||
|
|
||
| #[@test] | ||
| public function existing_partial() { | ||
| $this->templates->add('layout', 'My layout'); | ||
| $this->assertEquals('My layout', $this->evaluate('{{#> layout}}My content{{/layout}}', [])); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function existing_partial_with_partial_block() { | ||
| $this->templates->add('layout', 'My layout {{> @partial-block }}'); | ||
| $this->assertEquals('My layout My content', $this->evaluate('{{#> layout}}My content{{/layout}}', [])); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function non_existant_partial_renders_default() { | ||
| $this->assertEquals('My content', $this->evaluate('{{#> layout}}My content{{/layout}}', [])); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php namespace com\handlebarsjs\unittest; | ||
|
|
||
| use com\handlebarsjs\Templates; | ||
|
|
||
| class TemplatesTest extends \unittest\TestCase { | ||
|
|
||
| #[@test] | ||
| public function can_create() { | ||
| new Templates(); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function source() { | ||
| $fixture= new Templates(); | ||
| $fixture->register('test', 'My content'); | ||
| $this->assertEquals('My content', $fixture->source('test')->code()); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function non_existant_source() { | ||
| $fixture= new Templates(); | ||
| $this->assertFalse($fixture->source('non-existant')->exists()); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function existant_source() { | ||
| $fixture= new Templates(); | ||
| $fixture->register('test', 'My content'); | ||
| $this->assertTrue($fixture->source('test')->exists()); | ||
| } | ||
|
|
||
| #[@test] | ||
| public function register_returns_previous() { | ||
| $fixture= new Templates(); | ||
|
|
||
| $prev= []; | ||
| $prev[]= $fixture->register('@partial-block', 'A'); | ||
| $prev[]= $fixture->register('@partial-block', 'B')->code(); | ||
|
|
||
| $this->assertEquals([null, 'A'], $prev); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pull request does not support generic decorators as e.g.
...just
*inlineas a special case! This is the same as the original implementation in handlebars-java