@@ -41,7 +41,7 @@ private function __construct()
41
41
add_filter ($ type . '_template_hierarchy ' , [$ this , 'addLatteTemplate ' ]);
42
42
}
43
43
44
- add_filter ('template_include ' , [$ this , 'templateInclude ' ]);
44
+ add_filter ('template_include ' , [$ this , 'render ' ]);
45
45
add_filter ('comments_template ' , [$ this , 'commentsTemplate ' ]);
46
46
add_filter ('theme_page_templates ' , [$ this , 'registerCustomTemplates ' ], 10 , 3 );
47
47
}
@@ -92,14 +92,6 @@ public static function initialize(): void
92
92
self ::getInstance ();
93
93
}
94
94
95
- /**
96
- * Renders $template
97
- */
98
- public static function render (string $ template , array $ params = []): void
99
- {
100
- self ::getInstance ()->templateInclude ($ template , $ params );
101
- }
102
-
103
95
/**
104
96
* Adds filter to Latte
105
97
*/
@@ -172,26 +164,49 @@ public function registerCustomTemplates(array $page_templates, \WP_Theme $theme,
172
164
return $ page_templates ;
173
165
}
174
166
167
+ private function prepareRenderParams (array $ additionalParams = []): array
168
+ {
169
+ // https://developer.wordpress.org/reference/functions/load_template/
170
+ global $ posts , $ post , $ wp_did_header , $ wp_query , $ wp_rewrite , $ wpdb , $ wp_version , $ wp , $ id , $ comment , $ user_ID ;
171
+ $ params = compact ('posts ' , 'post ' , 'wp_did_header ' , 'wp_query ' , 'wp_rewrite ' , 'wpdb ' , 'wp_version ' , 'wp ' , 'id ' , 'comment ' , 'user_ID ' );
172
+
173
+ if (is_array ($ wp_query ->query_vars )) {
174
+ $ params = array_merge ($ wp_query ->query_vars , $ params );
175
+ }
176
+
177
+ return array_merge ($ params , $ additionalParams );
178
+ }
179
+
175
180
/**
176
181
* Renders template
177
182
*/
178
- public function templateInclude (string $ template , array $ additionalParams = []): string
183
+ public static function render (string $ template , array $ additionalParams = []): string
179
184
{
180
185
if (preg_match ('/\.latte$/m ' , $ template )) {
181
- // https://developer.wordpress.org/reference/functions/load_template/
182
- global $ posts , $ post , $ wp_did_header , $ wp_query , $ wp_rewrite , $ wpdb , $ wp_version , $ wp , $ id , $ comment , $ user_ID ;
183
- $ params = compact ('posts ' , 'post ' , 'wp_did_header ' , 'wp_query ' , 'wp_rewrite ' , 'wpdb ' , 'wp_version ' , 'wp ' , 'id ' , 'comment ' , 'user_ID ' );
186
+ self ::getInstance ()->engine ->render (
187
+ $ template ,
188
+ self ::getInstance ()->prepareRenderParams ($ additionalParams )
189
+ );
184
190
185
- if (is_array ($ wp_query ->query_vars )) {
186
- $ params = array_merge ($ wp_query ->query_vars , $ params );
187
- }
191
+ return self ::getInstance ()->emptyTemplate ;
192
+ }
188
193
189
- $ this ->engine ->render ($ template , array_merge ($ params , $ additionalParams ));
194
+ return $ template ;
195
+ }
190
196
191
- return $ this ->emptyTemplate ;
197
+ /**
198
+ * Renders template to string
199
+ */
200
+ public static function renderToString (string $ template , array $ additionalParams = []): string
201
+ {
202
+ if (preg_match ('/\.latte$/m ' , $ template )) {
203
+ return self ::getInstance ()->engine ->renderToString (
204
+ $ template ,
205
+ self ::getInstance ()->prepareRenderParams ($ additionalParams )
206
+ );
192
207
}
193
208
194
- return $ template ;
209
+ return '' ;
195
210
}
196
211
197
212
/**
@@ -206,7 +221,7 @@ public function commentsTemplate(string $commentTemplate): string
206
221
}
207
222
208
223
if (file_exists ($ latteTemplate )) {
209
- $ commentTemplate = $ this ->templateInclude ($ latteTemplate );
224
+ $ commentTemplate = $ this ->render ($ latteTemplate );
210
225
}
211
226
212
227
return $ commentTemplate ;
0 commit comments