Issue #3192830 by neclimdul: twig_render_template micro optimization

(cherry picked from commit 0ba6b793c6)
merge-requests/8642/head
Dave Long 2024-06-07 23:46:24 +01:00
parent ac1bdc7900
commit d7549f3c3a
No known key found for this signature in database
GPG Key ID: ED52AE211E142771
1 changed files with 11 additions and 9 deletions

View File

@ -29,14 +29,8 @@ function twig_extension() {
function twig_render_template($template_file, array $variables) { function twig_render_template($template_file, array $variables) {
/** @var \Twig\Environment $twig_service */ /** @var \Twig\Environment $twig_service */
$twig_service = \Drupal::service('twig'); $twig_service = \Drupal::service('twig');
$output = [
'debug_prefix' => '',
'debug_info' => '',
'rendered_markup' => '',
'debug_suffix' => '',
];
try { try {
$output['rendered_markup'] = $twig_service->load($template_file)->render($variables); $rendered_markup = $twig_service->load($template_file)->render($variables);
} }
catch (RuntimeError $e) { catch (RuntimeError $e) {
// In case there is a previous exception, re-throw the previous exception, // In case there is a previous exception, re-throw the previous exception,
@ -49,6 +43,13 @@ function twig_render_template($template_file, array $variables) {
throw $e; throw $e;
} }
if ($twig_service->isDebug()) { if ($twig_service->isDebug()) {
$output = [
'debug_prefix' => '',
'debug_info' => '',
'rendered_markup' => $rendered_markup,
'debug_suffix' => '',
];
$output['debug_prefix'] .= "\n\n<!-- THEME DEBUG -->"; $output['debug_prefix'] .= "\n\n<!-- THEME DEBUG -->";
$output['debug_prefix'] .= "\n<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->"; $output['debug_prefix'] .= "\n<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
// If there are theme suggestions, reverse the array so more specific // If there are theme suggestions, reverse the array so more specific
@ -110,7 +111,8 @@ function twig_render_template($template_file, array $variables) {
} }
$output['debug_info'] .= "\n<!-- " . $template_override_status_output . " from '" . Html::escape($template_file) . "' -->\n"; $output['debug_info'] .= "\n<!-- " . $template_override_status_output . " from '" . Html::escape($template_file) . "' -->\n";
$output['debug_suffix'] .= "\n<!-- " . $template_override_suffix_output . " from '" . Html::escape($template_file) . "' -->\n\n"; $output['debug_suffix'] .= "\n<!-- " . $template_override_suffix_output . " from '" . Html::escape($template_file) . "' -->\n\n";
}
// This output has already been rendered and is therefore considered safe. // This output has already been rendered and is therefore considered safe.
return Markup::create(implode('', $output)); return Markup::create(implode('', $output));
} }
return Markup::create($rendered_markup);
}