Skip to content

Commit 0a9e561

Browse files
committed
add renderToString method
1 parent 1590520 commit 0a9e561

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/NetteLatteEngine.php

+35-20
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private function __construct()
4141
add_filter($type . '_template_hierarchy', [$this, 'addLatteTemplate']);
4242
}
4343

44-
add_filter('template_include', [$this, 'templateInclude']);
44+
add_filter('template_include', [$this, 'render']);
4545
add_filter('comments_template', [$this, 'commentsTemplate']);
4646
add_filter('theme_page_templates', [$this, 'registerCustomTemplates'], 10, 3);
4747
}
@@ -92,14 +92,6 @@ public static function initialize(): void
9292
self::getInstance();
9393
}
9494

95-
/**
96-
* Renders $template
97-
*/
98-
public static function render(string $template, array $params = []): void
99-
{
100-
self::getInstance()->templateInclude($template, $params);
101-
}
102-
10395
/**
10496
* Adds filter to Latte
10597
*/
@@ -172,26 +164,49 @@ public function registerCustomTemplates(array $page_templates, \WP_Theme $theme,
172164
return $page_templates;
173165
}
174166

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+
175180
/**
176181
* Renders template
177182
*/
178-
public function templateInclude(string $template, array $additionalParams = []): string
183+
public static function render(string $template, array $additionalParams = []): string
179184
{
180185
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+
);
184190

185-
if (is_array($wp_query->query_vars)) {
186-
$params = array_merge($wp_query->query_vars, $params);
187-
}
191+
return self::getInstance()->emptyTemplate;
192+
}
188193

189-
$this->engine->render($template, array_merge($params, $additionalParams));
194+
return $template;
195+
}
190196

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+
);
192207
}
193208

194-
return $template;
209+
return '';
195210
}
196211

197212
/**
@@ -206,7 +221,7 @@ public function commentsTemplate(string $commentTemplate): string
206221
}
207222

208223
if (file_exists($latteTemplate)) {
209-
$commentTemplate = $this->templateInclude($latteTemplate);
224+
$commentTemplate = $this->render($latteTemplate);
210225
}
211226

212227
return $commentTemplate;

0 commit comments

Comments
 (0)