dhii / php-template
A concrete PHP (PHTML) template implementation.
Installs: 3 871
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 5
Requires
- php: ^7.0
- dhii/output-renderer-interface: ^0.4
Requires (Dev)
- dhii/stringable-interface: ^0.1
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^6.0 || ^7.0
- psr/container: ^1.0
This package is auto-updated.
Last update: 2024-10-20 16:29:17 UTC
README
A concrete PHP (PHTML) template implementation.
Details
This is an implementation of the Dhii output renderer standard; 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:
- The file at path
template.php
is used to produce the output. - Context members are retrieved by
$c('key')
. - It is possible to use the
uc
function with$f('uc')
. - 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
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
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
/* @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
<span class="current-time">1586364371<span /> <span class="username">jcdenton</span><br /> <span class="password">bionicman</span><br /> <span class="status">DEFECTED</span>