Skip to content

Commit 9d8fe3a

Browse files
committed
ApplicationLatte: Template divided into hierarchy LatteTemplate > Template (dynamic) + StrictTemplate
1 parent 85c27ec commit 9d8fe3a

File tree

5 files changed

+196
-125
lines changed

5 files changed

+196
-125
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Bridges\ApplicationLatte;
11+
12+
use Latte;
13+
use Nette;
14+
15+
16+
/**
17+
* Latte powered template.
18+
*/
19+
abstract class LatteTemplate implements Nette\Application\UI\ITemplate
20+
{
21+
/** @var Latte\Engine */
22+
private $latte;
23+
24+
/** @var string */
25+
private $file;
26+
27+
28+
public function __construct(Latte\Engine $latte)
29+
{
30+
$this->latte = $latte;
31+
}
32+
33+
34+
final public function getLatte(): Latte\Engine
35+
{
36+
return $this->latte;
37+
}
38+
39+
40+
/**
41+
* Renders template to output.
42+
*/
43+
public function render(string $file = null, array $params = []): void
44+
{
45+
$this->latte->render($file ?: $this->file, $params + $this->getParameters());
46+
}
47+
48+
49+
/**
50+
* Renders template to output.
51+
*/
52+
public function renderToString(string $file = null, array $params = []): string
53+
{
54+
return $this->latte->renderToString($file ?: $this->file, $params + $this->getParameters());
55+
}
56+
57+
58+
/**
59+
* Renders template to string.
60+
* @param can throw exceptions? (hidden parameter)
61+
*/
62+
public function __toString(): string
63+
{
64+
try {
65+
return $this->latte->renderToString($this->file, $this->getParameters());
66+
} catch (\Throwable $e) {
67+
if (func_num_args() || PHP_VERSION_ID >= 70400) {
68+
throw $e;
69+
}
70+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
71+
return '';
72+
}
73+
}
74+
75+
76+
/********************* template filters & helpers ****************d*g**/
77+
78+
79+
/**
80+
* Registers run-time filter.
81+
* @return static
82+
*/
83+
public function addFilter(?string $name, callable $callback)
84+
{
85+
$this->latte->addFilter($name, $callback);
86+
return $this;
87+
}
88+
89+
90+
/**
91+
* Registers run-time function.
92+
* @return static
93+
*/
94+
public function addFunction(string $name, callable $callback)
95+
{
96+
$this->latte->addFunction($name, $callback);
97+
return $this;
98+
}
99+
100+
101+
/**
102+
* Sets translate adapter.
103+
* @return static
104+
*/
105+
public function setTranslator(?Nette\Localization\ITranslator $translator)
106+
{
107+
$this->latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator): string {
108+
return $translator === null ? $args[0] : $translator->translate(...$args);
109+
});
110+
return $this;
111+
}
112+
113+
114+
/********************* template parameters ****************d*g**/
115+
116+
117+
/**
118+
* Sets the path to the template file.
119+
* @return static
120+
*/
121+
public function setFile(string $file)
122+
{
123+
$this->file = $file;
124+
return $this;
125+
}
126+
127+
128+
final public function getFile(): ?string
129+
{
130+
return $this->file;
131+
}
132+
133+
134+
/**
135+
* Returns array of all parameters.
136+
*/
137+
abstract public function getParameters(): array;
138+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Bridges\ApplicationLatte;
11+
12+
use Nette;
13+
14+
15+
/**
16+
* Strict Latte powered template for controls.
17+
*/
18+
class StrictTemplate extends LatteTemplate
19+
{
20+
use Nette\SmartObject;
21+
22+
/** @var Nette\Security\User */
23+
public $user;
24+
25+
/** @var string */
26+
public $baseUrl;
27+
28+
/** @var string */
29+
public $basePath;
30+
31+
/** @var array */
32+
public $flashes = [];
33+
34+
/** @var Nette\Application\UI\Presenter|null */
35+
public $presenter;
36+
37+
/** @var Nette\Application\UI\Control|null */
38+
public $control;
39+
40+
41+
/**
42+
* Returns array of all parameters.
43+
*/
44+
public function getParameters(): array
45+
{
46+
return get_object_vars($this);
47+
}
48+
}

src/Bridges/ApplicationLatte/Template.php

Lines changed: 6 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -9,133 +9,18 @@
99

1010
namespace Nette\Bridges\ApplicationLatte;
1111

12-
use Latte;
1312
use Nette;
1413

1514

1615
/**
17-
* Latte powered template.
16+
* Dynamic Latte powered template.
1817
*/
19-
class Template implements Nette\Application\UI\ITemplate
18+
class Template extends LatteTemplate
2019
{
21-
use Nette\SmartObject;
22-
23-
/** @var Latte\Engine */
24-
private $latte;
25-
26-
/** @var string */
27-
private $file;
28-
2920
/** @var array */
3021
private $params = [];
3122

3223

33-
public function __construct(Latte\Engine $latte)
34-
{
35-
$this->latte = $latte;
36-
}
37-
38-
39-
final public function getLatte(): Latte\Engine
40-
{
41-
return $this->latte;
42-
}
43-
44-
45-
/**
46-
* Renders template to output.
47-
*/
48-
public function render(string $file = null, array $params = []): void
49-
{
50-
$this->latte->render($file ?: $this->file, $params + $this->params);
51-
}
52-
53-
54-
/**
55-
* Renders template to output.
56-
*/
57-
public function renderToString(string $file = null, array $params = []): string
58-
{
59-
return $this->latte->renderToString($file ?: $this->file, $params + $this->params);
60-
}
61-
62-
63-
/**
64-
* Renders template to string.
65-
* @param can throw exceptions? (hidden parameter)
66-
*/
67-
public function __toString(): string
68-
{
69-
try {
70-
return $this->latte->renderToString($this->file, $this->params);
71-
} catch (\Throwable $e) {
72-
if (func_num_args() || PHP_VERSION_ID >= 70400) {
73-
throw $e;
74-
}
75-
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
76-
return '';
77-
}
78-
}
79-
80-
81-
/********************* template filters & helpers ****************d*g**/
82-
83-
84-
/**
85-
* Registers run-time filter.
86-
* @return static
87-
*/
88-
public function addFilter(?string $name, callable $callback)
89-
{
90-
$this->latte->addFilter($name, $callback);
91-
return $this;
92-
}
93-
94-
95-
/**
96-
* Registers run-time function.
97-
* @return static
98-
*/
99-
public function addFunction(string $name, callable $callback)
100-
{
101-
$this->latte->addFunction($name, $callback);
102-
return $this;
103-
}
104-
105-
106-
/**
107-
* Sets translate adapter.
108-
* @return static
109-
*/
110-
public function setTranslator(?Nette\Localization\ITranslator $translator)
111-
{
112-
$this->latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator): string {
113-
return $translator === null ? $args[0] : $translator->translate(...$args);
114-
});
115-
return $this;
116-
}
117-
118-
119-
/********************* template parameters ****************d*g**/
120-
121-
122-
/**
123-
* Sets the path to the template file.
124-
* @return static
125-
*/
126-
public function setFile(string $file)
127-
{
128-
$this->file = $file;
129-
return $this;
130-
}
131-
132-
133-
final public function getFile(): ?string
134-
{
135-
return $this->file;
136-
}
137-
138-
13924
/**
14025
* Adds new template parameter.
14126
* @return static
@@ -164,7 +49,7 @@ public function setParameters(array $params)
16449
/**
16550
* Returns array of all parameters.
16651
*/
167-
final public function getParameters(): array
52+
public function getParameters(): array
16853
{
16954
return $this->params;
17055
}
@@ -173,7 +58,7 @@ final public function getParameters(): array
17358
/**
17459
* Sets a template parameter. Do not call directly.
17560
*/
176-
public function __set($name, $value): void
61+
public function __set(string $name, $value): void
17762
{
17863
$this->params[$name] = $value;
17964
}
@@ -183,7 +68,7 @@ public function __set($name, $value): void
18368
* Returns a template parameter. Do not call directly.
18469
* @return mixed value
18570
*/
186-
public function &__get($name)
71+
public function &__get(string $name)
18772
{
18873
if (!array_key_exists($name, $this->params)) {
18974
trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
@@ -196,7 +81,7 @@ public function &__get($name)
19681
/**
19782
* Determines whether parameter is defined. Do not call directly.
19883
*/
199-
public function __isset($name)
84+
public function __isset(string $name): bool
20085
{
20186
return isset($this->params[$name]);
20287
}

src/Bridges/ApplicationLatte/TemplateFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TemplateFactory implements UI\ITemplateFactory
2121
{
2222
use Nette\SmartObject;
2323

24-
/** @var callable[]&(callable(Template $template): void)[]; Occurs when a new template is created */
24+
/** @var callable[]&(callable(LatteTemplate $template): void)[]; Occurs when a new template is created */
2525
public $onCreate;
2626

2727
/** @var ILatteFactory */
@@ -47,8 +47,8 @@ public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $ht
4747
$this->httpRequest = $httpRequest;
4848
$this->user = $user;
4949
$this->cacheStorage = $cacheStorage;
50-
if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, Template::class, true))) {
51-
throw new Nette\InvalidArgumentException("Class $templateClass does not extend " . Template::class . ' or it does not exist.');
50+
if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, LatteTemplate::class, true))) {
51+
throw new Nette\InvalidArgumentException("Class $templateClass does not extend " . LatteTemplate::class . ' or it does not exist.');
5252
}
5353
$this->templateClass = $templateClass ?: Template::class;
5454
}

tests/Bridges.Latte/TemplateFactory.customTemplate.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test(function () {
5151

5252
Assert::exception(function () {
5353
$factory = new TemplateFactory(Mockery::mock(ILatteFactory::class), null, null, null, stdClass::class);
54-
}, \Nette\InvalidArgumentException::class, 'Class stdClass does not extend Nette\Bridges\ApplicationLatte\Template or it does not exist.');
54+
}, \Nette\InvalidArgumentException::class, 'Class stdClass does not extend Nette\Bridges\ApplicationLatte\LatteTemplate or it does not exist.');
5555

5656

5757
test(function () {

0 commit comments

Comments
 (0)