Skip to content

Commit

Permalink
TemplateFactory: added support for custom Template implementation [Cl…
Browse files Browse the repository at this point in the history
…oses #159][Closes #141]

With this change it's possible to simply use custom Template implementation. Usage example in config.neon:

latte:
    templateClass: App\CustomTemplateImplementation

Custom template  MUST extend Nette\Bridges\ApplicationLatte\Template.
  • Loading branch information
mrtnzlml authored and dg committed Dec 20, 2016
1 parent 25b3a34 commit 58a8d22
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LatteExtension extends Nette\DI\CompilerExtension
public $defaults = [
'xhtml' => FALSE,
'macros' => [],
'templateClass' => NULL,
];

/** @var bool */
Expand Down Expand Up @@ -54,7 +55,8 @@ public function loadConfiguration()

$builder->addDefinition($this->prefix('templateFactory'))
->setClass(Nette\Application\UI\ITemplateFactory::class)
->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class);
->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class)
->setArguments(['templateClass' => $config['templateClass']]);

foreach ($config['macros'] as $macro) {
$this->addMacro($macro);
Expand Down
11 changes: 9 additions & 2 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ class TemplateFactory implements UI\ITemplateFactory
/** @var Nette\Caching\IStorage */
private $cacheStorage;

/** @var string */
private $templateClass;


public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = NULL,
Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL)
Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL, $templateClass = NULL)
{
$this->latteFactory = $latteFactory;
$this->httpRequest = $httpRequest;
$this->user = $user;
$this->cacheStorage = $cacheStorage;
if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, Template::class, TRUE))) {
throw new Nette\InvalidArgumentException("Class $templateClass does not extend " . Template::class . ' or it does not exist.');
}
$this->templateClass = $templateClass ?: Template::class;
}


Expand All @@ -48,7 +55,7 @@ public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $ht
public function createTemplate(UI\Control $control = NULL)
{
$latte = $this->latteFactory->create();
$template = new Template($latte);
$template = new $this->templateClass($latte);
$presenter = $control ? $control->getPresenter(FALSE) : NULL;

if ($control instanceof UI\Presenter) {
Expand Down
76 changes: 76 additions & 0 deletions tests/Bridges.Latte/TemplateFactory.customTemplate.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Test: TemplateFactory custom template
*/

use Nette\Application\UI;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Bridges\ApplicationLatte\TemplateFactory;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class LatteFactoryMock implements Nette\Bridges\ApplicationLatte\ILatteFactory
{
private $engine;

public function __construct(Latte\Engine $engine)
{
$this->engine = $engine;
}

public function create()
{
return $this->engine;
}
}

class TemplateMockWithoutImplement
{

}

class TemplateMock extends Nette\Bridges\ApplicationLatte\Template
{
private $file = 'ko';

public function render()
{
return strrev($this->file);
}

public function setFile($file)
{
$this->file = $file;
}

public function getFile()
{
return $this->file;
}
}


test(function () {
$factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine));
Assert::type(Template::class, $factory->createTemplate());
});

Assert::exception(function () {
$factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine), NULL, NULL, NULL, TemplateMockWithoutImplement::class);
}, \Nette\InvalidArgumentException::class, 'Class TemplateMockWithoutImplement does not extend Nette\Bridges\ApplicationLatte\Template or it does not exist.');


test(function () {
$factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine), NULL, NULL, NULL, TemplateMock::class);
$template = $factory->createTemplate();
Assert::type(TemplateMock::class, $template);
Assert::type(UI\ITemplate::class, $template);
Assert::same([], $template->flashes);
Assert::same('ok', $template->render());
$template->setFile('bla');
Assert::same('alb', $template->render());
});

0 comments on commit 58a8d22

Please sign in to comment.