Skip to content

Commit

Permalink
add class DefaultDataTemplate (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
slepic authored Dec 11, 2020
1 parent 481f20f commit 0b24d5a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea
.php_cs.cache
composer.lock
.phpunit.result.cache
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
# php-template
Template rendering abstraction library. Abstract your libraries from specific templating engines.

This library provides the [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php), which is the abstraction of any data rendering template.
And it also provides one simple implementation called [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php), which renders the data using another PHP script and PHP ob_* functions.

## Requirements

PHP 7.4 or 8
Expand All @@ -17,6 +14,19 @@ Install with composer

```composer require slepic/php-template```

## Interfaces

### [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php)
This is the abstraction of any data rendering template.

## Classes

### [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php)
A simple template implementation, which renders the data using another PHP script (given its filename) and PHP ob_* functions.

### [```DefaultDataTemplate```](https://github.com/slepic/php-template/blob/master/src/DefaultDataTemplate.php)
Template decorator which allows to feed your templates with default data hidden from the template consumer.

## Contribution

If you create a library that depends on this one and you use composer, please consider the following:
Expand All @@ -25,6 +35,11 @@ If you create a library that depends on this one and you use composer, please co

## Changelog

### 1.1.0
* added new class `DefaultDataTemplate`
* `OutputBufferTemplate` now ends the output buffer if the included template throws an exception.
* `OutputBufferTemplate` now uses `include` instead of `require` to execute the template script .

### 1.0.0

* bump PHP to ^7.4 || ^8.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Simple PHP abstraction of template rendering.",
"type": "library",
"require": {
"php": "^7.4 || ^8.0"
"php": ">=7.4"
},
"autoload": {
"psr-4": {
Expand Down
35 changes: 35 additions & 0 deletions src/DefaultDataTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Slepic\Templating\Template;

/**
* An implementation of TemplateInterface which wraps another implementation and keeps some default data
* which it then supplies to the inner template upon rendering.
*
* Overriding default values is allowed.
* A simple array_merge is used and so nested arrays are not merged recursively.
* However, this may change in future.
*/
class DefaultDataTemplate implements TemplateInterface
{
private TemplateInterface $template;
private array $data;

/**
* @param TemplateInterface $template
* @param array<string,mixed> $data
*/
public function __construct(TemplateInterface $template, array $data)
{
$this->template = $template;
$this->data = $data;
}

public function render(array $data): string
{
$allData = \array_merge($this->data, $data);
return $this->template->render($allData);
}
}
9 changes: 8 additions & 1 deletion src/OutputBufferTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ public function render(array $data): string
'Expected associative array where keys are valid variable names.'
);
}

\ob_start();
require($this->templateFile);
try {
include($this->templateFile);
} catch (\Throwable $e) {
\ob_end_clean();
throw $e;
}

return \ob_get_clean();
}
}
2 changes: 1 addition & 1 deletion src/TemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface TemplateInterface
/**
* Renders the template with given data.
*
* @param array $data
* @param array<string,mixed> $data
* @return string
*/
public function render(array $data): string;
Expand Down
57 changes: 57 additions & 0 deletions tests/DefaultDataTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Slepic\Tests\Templating\Template;

use PHPUnit\Framework\TestCase;
use Slepic\Templating\Template\DefaultDataTemplate;
use Slepic\Templating\Template\TemplateInterface;

final class DefaultDataTemplateTest extends TestCase
{
public function testImplements(): void
{
$innerTemplate = self::createMock(TemplateInterface::class);
$template = new DefaultDataTemplate($innerTemplate, []);
$this->assertInstanceOf(TemplateInterface::class, $template);
}

/**
* @param array $defaultData
* @param array $data
* @param array $allData
*
* @dataProvider provideRenderData
*/
public function testRender(array $defaultData, array $data, array $allData): void
{
$hash = \md5((string) \time());
$innerTemplate = self::createMock(TemplateInterface::class);
$innerTemplate->expects(self::once())
->method('render')
->with($allData)
->willReturn($hash);
$template = new DefaultDataTemplate($innerTemplate, $defaultData);
$output = $template->render($data);
self::assertSame($hash, $output);
}

public function provideRenderData(): array
{
$hash = \md5((string) \time());
return [
[[], [], []],
[
['default' => 'value'],
['test' => $hash],
['test' => $hash, 'default' => 'value']
],
[
['override' => 'default'],
['keep' => $hash, 'override' => 'overridden'],
['keep' => $hash, 'override' => 'overridden']
],
];
}
}

0 comments on commit 0b24d5a

Please sign in to comment.