-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor template loading mechanism #8
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 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8294cf3
Refactor template loading mechanism
thekid 4a5716d
Fix error message
thekid 47b818c
Fix withTemplates() parameter type
thekid b71a0ab
Use base interface, Input
thekid c94b931
QA: Use type unions in evaluate()
thekid 03a9c75
QA: Whitespace
thekid a110a5a
Implement listing() method from WithListing
thekid 388e7bc
QA: Use type unions in render() and transform()
thekid b48e9c3
Remove WithListing interface from Templates implementations
thekid 342ce58
QA: Fix load() api docs
thekid 02cfacb
Restore BC by making Templates implement TemplateLoader
thekid 131382b
QA: Deprecation markers
thekid e732e3f
Add possibility to store compiled templates in template sources
thekid 27d17cf
Refactor $template arguments to $source, clarifying intent
thekid 40f7e9d
Refactor exists() method to base class
thekid 67d9aef
QA: Apidocs wording
thekid 9c1960a
Rename Input -> Tokens
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
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| <?php namespace com\github\mustache; | ||
|
|
||
| use text\StreamTokenizer; | ||
| use com\github\mustache\templates\Input; | ||
| use com\github\mustache\templates\Templates; | ||
| use com\github\mustache\templates\FromLoader; | ||
| use text\StringTokenizer; | ||
|
|
||
| /** | ||
|
|
@@ -23,8 +25,7 @@ | |
| * @see http://mustache.github.io/mustache.5.html | ||
| */ | ||
| class MustacheEngine { | ||
| protected $templates; | ||
| protected $parser; | ||
| protected $templates, $parser; | ||
| public $helpers= []; | ||
|
|
||
| /** | ||
|
|
@@ -38,18 +39,22 @@ public function __construct() { | |
| /** | ||
| * Sets template loader to be used | ||
| * | ||
| * @param com.github.mustache.TemplateLoader $l | ||
| * @param com.github.mustache.templates.Templates|com.github.mustache.TemplateLoader $l | ||
| * @return self this | ||
| */ | ||
| public function withTemplates(TemplateLoader $l) { | ||
| $this->templates= $l; | ||
| public function withTemplates($l) { | ||
| if ($l instanceof Templates) { | ||
| $this->templates= $l; | ||
| } else { | ||
| $this->templates= new FromLoader($l); | ||
| } | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets template loader to be used | ||
| * Gets used template loader | ||
| * | ||
| * @return com.github.mustache.TemplateLoader | ||
| * @return com.github.mustache.Templates | ||
| */ | ||
| public function getTemplates() { | ||
| return $this->templates; | ||
|
|
@@ -92,15 +97,15 @@ public function withHelpers(array $helpers) { | |
| /** | ||
| * Compile a template. | ||
| * | ||
| * @param string $template The template, as a string | ||
| * @param string|com.github.mustache.templates.Input $template The template | ||
| * @param string $start Initial start tag, defaults to "{{" | ||
| * @param string $end Initial end tag, defaults to "}}" | ||
| * @param string $indent Indenting level, defaults to no indenting | ||
| * @return com.github.mustache.Template | ||
| */ | ||
| public function compile($template, $start= '{{', $end= '}}', $indent= '') { | ||
| return new Template('<string>', $this->parser->parse( | ||
| new StringTokenizer($template), | ||
| $template instanceof Input ? $template->tokens() : new StringTokenizer($template), | ||
| $start, | ||
| $end, | ||
| $indent | ||
|
|
@@ -118,7 +123,7 @@ public function compile($template, $start= '{{', $end= '}}', $indent= '') { | |
| */ | ||
| public function load($name, $start= '{{', $end= '}}', $indent= '') { | ||
| return new Template($name, $this->parser->parse( | ||
| new StreamTokenizer($this->templates->load($name)), | ||
| $this->templates->load($name)->tokens(), | ||
| $start, | ||
| $end, | ||
| $indent | ||
|
|
@@ -142,22 +147,20 @@ public function evaluate(Template $template, $arg) { | |
| } | ||
|
|
||
| /** | ||
| * Render a template - like evaluate(), but will compile if necessary. | ||
| * Render a template, compiling it from source | ||
| * | ||
| * @param var $template The template, either as string or as compiled Template instance | ||
| * @param string|com.github.mustache.templates.Input $template | ||
| * @param var $arg Either a view context, or a Context instance | ||
| * @param string $start Initial start tag, defaults to "{{" | ||
| * @param string $end Initial end tag, defaults to "}}" | ||
| * @param string $indent Indenting level, defaults to no indenting | ||
| * @return string The rendered output | ||
| */ | ||
| public function render($template, $arg, $start= '{{', $end= '}}', $indent= '') { | ||
| if ($template instanceof Template) { | ||
|
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 was dropped, use |
||
| $target= $template; | ||
| } else { | ||
| $target= $this->compile($template, $start, $end, $indent); | ||
| } | ||
| return $this->evaluate($target, $arg); | ||
| return $this->evaluate( | ||
| $this->compile($template, $start, $end, $indent), | ||
| $arg | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
31 changes: 31 additions & 0 deletions
31
src/main/php/com/github/mustache/templates/FromLoader.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,31 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| use text\StreamTokenizer; | ||
|
|
||
| /** | ||
| * Adapter for TemplateLoaders | ||
| * | ||
| * @deprecated Template loaders were replaced by `Templates`. | ||
| */ | ||
| class FromLoader implements Templates { | ||
| private $loader; | ||
|
|
||
| /** @param com.github.mustache.TemplateLoader $loader */ | ||
| public function __construct($loader) { | ||
| $this->loader= $loader; | ||
| } | ||
|
|
||
| /** | ||
| * Load a template by a given name | ||
| * | ||
| * @param string $name The template name, not including the file extension | ||
| * @return com.github.mustache.TemplateSource | ||
| */ | ||
| public function load($name) { | ||
| try { | ||
| return new Source(new StreamTokenizer($this->loader->load($name))); | ||
| } catch (TemplateNotFoundException $e) { | ||
| return new NotFound($e->getMessage()); | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/php/com/github/mustache/templates/Input.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,15 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| interface Input { | ||
|
|
||
| /** @return bool */ | ||
| public function exists(); | ||
|
|
||
| /** | ||
| * Returns tokens | ||
| * | ||
| * @return text.Tokenizer | ||
| * @throws com.github.mustache.TemplateNotFoundException | ||
| */ | ||
| public function tokens(); | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/php/com/github/mustache/templates/NotFound.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,23 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| use com\github\mustache\TemplateNotFoundException; | ||
|
|
||
| class NotFound implements Input { | ||
| private $reason; | ||
|
|
||
| /** @param string $reason */ | ||
| public function __construct($reason) { | ||
| $this->reason= $reason; | ||
| } | ||
|
|
||
| /** @return bool */ | ||
| public function exists() { return false; } | ||
|
|
||
| /** | ||
| * Returns tokens | ||
| * | ||
| * @return text.Tokenizer | ||
| * @throws com.github.mustache.TemplateNotFoundException | ||
| */ | ||
| public function tokens() { throw new TemplateNotFoundException($this->reason); } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/php/com/github/mustache/templates/Source.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\github\mustache\templates; | ||
|
|
||
| class Source implements Input { | ||
| private $tokens; | ||
|
|
||
| /** @param text.Tokenizer $tokens */ | ||
| public function __construct($tokens) { | ||
| $this->tokens= $tokens; | ||
| } | ||
|
|
||
| /** @return bool */ | ||
| public function exists() { return true; } | ||
|
|
||
| /** | ||
| * Returns tokens | ||
| * | ||
| * @return text.Tokenizer | ||
| * @throws com.github.mustache.TemplateNotFoundException | ||
| */ | ||
| public function tokens() { return $this->tokens; } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/php/com/github/mustache/templates/Templates.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,18 @@ | ||
| <?php namespace com\github\mustache\templates; | ||
|
|
||
| /** | ||
| * Template loading | ||
| * | ||
| * @test xp://com.github.mustache.unittest.TemplateTransformationTest | ||
| */ | ||
| interface Templates { | ||
|
|
||
| /** | ||
| * Load a template by a given name | ||
| * | ||
| * @param string $name The template name, not including the file extension | ||
| * @return com.github.mustache.templates.Source | ||
| */ | ||
| public function load($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
8 changes: 3 additions & 5 deletions
8
src/test/php/com/github/mustache/unittest/InMemoryTest.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
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
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.
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.
The
WithListinginterface should be merged into theTemplatesclass.