99
1010namespace Nette \Bridges \ApplicationLatte ;
1111
12- use Latte ;
1312use 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 }
0 commit comments