Verified Commit ffdafb02 authored by Dave Long's avatar Dave Long
Browse files

Issue #3192830 by neclimdul: twig_render_template micro optimization

(cherry picked from commit 0ba6b793)
parent d9111a47
Loading
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -29,14 +29,8 @@ function twig_extension() {
function twig_render_template($template_file, array $variables) {
  /** @var \Twig\Environment $twig_service */
  $twig_service = \Drupal::service('twig');
  $output = [
    'debug_prefix' => '',
    'debug_info' => '',
    'rendered_markup' => '',
    'debug_suffix' => '',
  ];
  try {
    $output['rendered_markup'] = $twig_service->load($template_file)->render($variables);
    $rendered_markup = $twig_service->load($template_file)->render($variables);
  }
  catch (RuntimeError $e) {
    // 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;
  }
  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<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
    // 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_suffix'] .= "\n<!-- " . $template_override_suffix_output . " from '" . Html::escape($template_file) . "' -->\n\n";
  }
    // This output has already been rendered and is therefore considered safe.
    return Markup::create(implode('', $output));
  }
  return Markup::create($rendered_markup);
}