-
Notifications
You must be signed in to change notification settings - Fork 0
Added Template Factory #2
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8de909b
Added template factory
XedinUnknown 3a24b46
Corrected readme badges
XedinUnknown dde354b
Add missing doc
XedinUnknown c770f0e
Add docs to readme
XedinUnknown 37deb69
Remove extra quote
XedinUnknown b812a41
Add missing uses and types to example
XedinUnknown 2ead413
Remove useless comment
XedinUnknown 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,89 @@ | ||
| # Dhii - Php Template | ||
|
|
||
| [](https://travis-ci.org/dhii/php-template) | ||
| [](https://codeclimate.com/github/Dhii/php-template) | ||
| [](https://codeclimate.com/github/Dhii/php-template/coverage) | ||
| [](https://packagist.org/packages/dhii/php-template) | ||
| [][Dhii] | ||
|
|
||
| A concrete PHP (PHTML) template implementation. | ||
|
|
||
| [Dhii]: https://github.com/Dhii/dhii | ||
| # Dhii - Php Template | ||
|
|
||
| [](https://travis-ci.org/dhii/php-template) | ||
| [](https://codeclimate.com/github/Dhii/php-template) | ||
| [](https://codeclimate.com/github/Dhii/php-template/coverage) | ||
| [](https://packagist.org/packages/dhii/php-template) | ||
|
|
||
| A concrete PHP (PHTML) template implementation. | ||
|
|
||
| ## Details | ||
| This is an implementation of the [Dhii output renderer standard][dhii/output-renderer-interface]; | ||
| specifically, the template. It allows consuming file-based PHP templates using an abstract interface. | ||
| Now it's possible to outsource your rendering to standardized, stateless PHP template files, | ||
| and use them just like any other template, without knowing where the content comes from. | ||
|
|
||
| Because this implementation is based on PHP files, it was decided to separate the concern | ||
| of rendering a template from the concern of evaluating a PHP file, because the latter | ||
| is useful on its own, and because it would make the template implementation thinner | ||
| and cleaner. | ||
|
|
||
| ### Usage | ||
| Below examples explain how a template factory could be configured, and used to produce a | ||
| standards-compliant template. Then that template is rendered with context. Please note the following: | ||
|
|
||
| 1. The file at path `template.php` is used to produce the output. | ||
| 2. Context members are retrieved by `$c('key')`. | ||
| 3. It is possible to use the `uc` function with `$f('uc')`. | ||
| 4. The default context member `time` is present in the template, even though it was not explicitly supplied | ||
| at render time. | ||
|
|
||
| #### Configuration, usually in a service definition | ||
| ```php | ||
| use Dhii\Output\PhpEvaluator\FilePhpEvaluatorFactory; | ||
| use Dhii\Output\Template\PhpTemplate\FilePathTemplateFactory; | ||
| use Dhii\Output\Template\PathTemplateFactoryInterface; | ||
|
|
||
| function (): PathTemplateFactoryInterface { | ||
| return new FilePathTemplateFactory( | ||
| new FilePhpEvaluatorFactory(), | ||
| [ // This will be available by default in all contexts of all templates made by this factory | ||
| 'time' => time(), // Let's assume it's 1586364371 | ||
| ], | ||
| [ // This will be available by default in all templates made by this factory | ||
| 'uc' => function (string $string) { | ||
| return strtoupper($string); | ||
| }, | ||
| ] | ||
| ); | ||
| }; | ||
| ``` | ||
|
|
||
| #### Consumption, usually somewhere in controller-level code | ||
| ```php | ||
| use Dhii\Output\Template\PathTemplateFactoryInterface; | ||
| use Dhii\Output\Template\PhpTemplate\FilePathTemplateFactory; | ||
|
|
||
| /* @var $fileTemplateFactory FilePathTemplateFactory */ | ||
| (function (PathTemplateFactoryInterface $factory) { | ||
| $template = $factory->fromPath('template.php'); | ||
| echo $template->render([ | ||
| 'username' => 'jcdenton', | ||
| 'password' => 'bionicman', | ||
| 'status' => 'defected', | ||
| ]); | ||
| })($fileTemplateFactory); // This is the factory created by above configuration | ||
| ``` | ||
|
|
||
| #### template.php | ||
| ```php | ||
| /* @var $c callable */ | ||
| /* @var $f callable */ | ||
| ?> | ||
| <span class="current-time"><?= $c('time') ?><span /> | ||
| <span class="username"><?= $c('username') ?></span><br /> | ||
| <span class="password"><?= $c('password') ?></span><br /> | ||
| <span class="status"><?= $f('uc', $c('status')) ?></span> | ||
| ``` | ||
|
|
||
| #### Resulting output | ||
| ```html | ||
| <span class="current-time">1586364371<span /> | ||
| <span class="username">jcdenton</span><br /> | ||
| <span class="password">bionicman</span><br /> | ||
| <span class="status">DEFECTED</span> | ||
| ``` | ||
|
|
||
|
|
||
| [Dhii]: https://github.com/Dhii/dhii | ||
| [dhii/output-renderer-interface]: https://travis-ci.org/Dhii/output-renderer-interface |
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,54 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Dhii\Output\Template\PhpTemplate; | ||
|
|
||
| use Dhii\Output\PhpEvaluator\FilePhpEvaluatorFactoryInterface; | ||
| use Dhii\Output\Template\PathTemplateFactoryInterface; | ||
| use Dhii\Output\Template\PhpTemplate; | ||
| use Dhii\Output\Template\TemplateInterface; | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| class FilePathTemplateFactory implements PathTemplateFactoryInterface | ||
| { | ||
| /** | ||
| * @var FilePhpEvaluatorFactoryInterface | ||
| */ | ||
| protected $evaluatorFactory; | ||
| /** | ||
| * @var array | ||
| */ | ||
| protected $defaultContext; | ||
| /** | ||
| * @var array | ||
| */ | ||
| protected $functions; | ||
|
|
||
| /** | ||
| * @param FilePhpEvaluatorFactoryInterface $evaluatorFactory A factory that creates PHP file evaluators. | ||
| * @param array $defaultContext A map of keys to values that will be available in template context by default. | ||
| * @param array $functions A map of keys to callables that will be available to templates by default. | ||
| */ | ||
| public function __construct( | ||
XedinUnknown marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| FilePhpEvaluatorFactoryInterface $evaluatorFactory, | ||
| array $defaultContext, | ||
| array $functions | ||
| ) { | ||
| $this->evaluatorFactory = $evaluatorFactory; | ||
| $this->defaultContext = $defaultContext; | ||
| $this->functions = $functions; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function fromPath(string $templatePath): TemplateInterface | ||
| { | ||
| $evaluator = $this->evaluatorFactory->fromFilePath($templatePath); | ||
| $template = new PhpTemplate($evaluator, $this->defaultContext, $this->functions); | ||
|
|
||
| return $template; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.