-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
890f1c6
Initial release: Partial block helper
thekid 5d9d96b
Replace template loader with implementation supporting `@partial-block`
thekid db97d3d
Add partial block helper
thekid 413ca52
Add possibility to test with templates
thekid bf1726b
Add test for parsing partial blocks
thekid 46df23d
Initial support for inline partials
thekid 9cff7f3
Fix return
thekid 0c4c560
QA: Add missing apidocs
thekid 034b6af
Add tests for Templates class
thekid d41ff92
Upgrade to xp-forge/mustache v5.1.0 and make use of new template load…
thekid 9f6a63d
Test node handling
thekid 377aba7
Fix listing()
thekid f11dd79
Add link to handlebars spec
thekid b662f32
Fix register() not working with Source instances
thekid 6e88762
Eagerly evaluate partial blocks, so nested inline partials get evaluated
thekid 7e7f438
Extend com.handlebarsjs.unittest.PartialBlockHelperTest::layout() test
thekid 1ef5167
Only evaluate `@partial-block` once
thekid 8d23ec6
Add tests for nested partial blocks
thekid 29517dd
Fix partial blocks within each
thekid 37ea0db
Add tests for contexts
thekid 5310886
Refactor and support decorators
thekid 3136259
Add Decoration class
thekid 27a7539
QA: Shorten invocation code
thekid 5e43601
Rename decorators() -> enter()
thekid 16a0ed6
Document Decorators
thekid da39668
Speed up decorator name lookup
thekid e292e24
Rename evaluate() -> enter()
thekid fd5efef
Do not run decorators twice
thekid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/php/com/handlebarsjs/InlinePartialHelper.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?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); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/php/com/handlebarsjs/PartialBlockHelper.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php namespace com\handlebarsjs; | ||
|
|
||
| use com\github\mustache\NodeList; | ||
| use com\github\mustache\TemplateNotFoundException; | ||
|
|
||
| /** | ||
| * 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(); | ||
|
|
||
| try { | ||
| $templates->register('@partial-block', $this->fn); | ||
| return $context->engine->transform($this->name, $context, $this->start, $this->end, ''); | ||
| } catch (TemplateNotFoundException $e) { | ||
|
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. It would be nicer if mustache template loaders would either:
Catching an exception for control flow does not feel right [TM]
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. |
||
| return $this->fn->evaluate($context); | ||
| } finally { | ||
| $templates->remove('@partial-block'); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php namespace com\handlebarsjs; | ||
|
|
||
| use com\github\mustache\TemplateLoader; | ||
| use com\github\mustache\TemplateNotFoundException; | ||
| use io\streams\MemoryInputStream; | ||
|
|
||
| class Templates implements TemplateLoader { | ||
| private $templates= []; | ||
| private $delegate; | ||
|
|
||
| public function delegate($delegate) { | ||
| $this->delegate= $delegate; | ||
| } | ||
|
|
||
| public function register($name, $content) { | ||
| $this->templates[$name]= $content; | ||
| } | ||
|
|
||
| public function remove($name) { | ||
| unset($this->templates[$name]); | ||
| } | ||
|
|
||
| /** | ||
| * Load a template by a given name | ||
| * | ||
| * @param string $name The template name without file extension | ||
| * @return io.streams.InputStream | ||
| * @throws com.github.mustache.TemplateNotFoundException | ||
| */ | ||
| public function load($name) { | ||
| if (isset($this->templates[$name])) { | ||
| return new MemoryInputStream($this->templates[$name]); | ||
| } else if ($this->delegate) { | ||
| return $this->delegate->load($name); | ||
| } else { | ||
| throw new TemplateNotFoundException('Cannot find template '.$name); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/test/php/com/handlebarsjs/unittest/InlinePartialHelperTest.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]])); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/php/com/handlebarsjs/unittest/PartialBlockHelperTest.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}}', [])); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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