PHPackages                             dhii/php-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dhii/php-template

ActiveLibrary

dhii/php-template
=================

A concrete PHP (PHTML) template implementation.

v0.0.0(5y ago)13.9k[4 issues](https://github.com/Dhii/php-template/issues)[1 PRs](https://github.com/Dhii/php-template/pulls)MITPHPPHP ^7.0CI failing

Since Jan 11Pushed 4y ago3 watchersCompare

[ Source](https://github.com/Dhii/php-template)[ Packagist](https://packagist.org/packages/dhii/php-template)[ RSS](/packages/dhii-php-template/feed)WikiDiscussions develop Synced 5d ago

READMEChangelog (1)Dependencies (5)Versions (5)Used By (0)

Dhii - Php Template
===================

[](#dhii---php-template)

[![Build Status](https://camo.githubusercontent.com/652576e62c1279dbdea8c596b13bad7650b914f06b5c8448ccb6ce97ccc97431/68747470733a2f2f7472617669732d63692e6f72672f646869692f7068702d74656d706c6174652e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/dhii/php-template)[![Code Climate](https://camo.githubusercontent.com/783bd77898abebf56abf01ecb7ac8275e115c3b626c2bfb358dbaf89d2c9c0b8/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f446869692f7068702d74656d706c6174652f6261646765732f6770612e737667)](https://codeclimate.com/github/Dhii/php-template)[![Test Coverage](https://camo.githubusercontent.com/d377195e9040bde39b4d5fbc5dcfa4bf6a7c75447f3acf746db0e2468a19dd5a/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f446869692f7068702d74656d706c6174652f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/Dhii/php-template/coverage)[![Latest Stable Version](https://camo.githubusercontent.com/a47834c0440f4f935ae7294d46b6d88c0549edb0fd35f61013b117a505d26b45/68747470733a2f2f706f7365722e707567782e6f72672f646869692f7068702d74656d706c6174652f76657273696f6e)](https://packagist.org/packages/dhii/php-template)

A concrete PHP (PHTML) template implementation.

Details
-------

[](#details)

This is an implementation of the [Dhii output renderer standard](https://travis-ci.org/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

[](#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

[](#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

[](#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

[](#templatephp)

```
/* @var $c callable */
/* @var $f callable */
?>
