You are here

function twig_render_template in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/themes/engines/twig/twig.engine \twig_render_template()

Implements hook_render_template().

Renders a Twig template.

If the Twig debug setting is enabled, HTML comments including _theme() call and template file name suggestions will surround the template markup.

Parameters

string $template_file: The file name of the template to render.

array $variables: A keyed array of variables that will appear in the output.

Return value

string|\Drupal\Component\Render\MarkupInterface The output generated by the template, plus any debug information.

1 call to twig_render_template()
TwigWhiteListTest::testWhiteListChaining in core/modules/system/src/Tests/Theme/TwigWhiteListTest.php
Tests white-listing of methods doesn't interfere with chaining.
1 string reference to 'twig_render_template'
ThemeManager::render in core/lib/Drupal/Core/Theme/ThemeManager.php
Generates themed output.

File

core/themes/engines/twig/twig.engine, line 51
Handles integration of Twig templates with the Drupal theme system.

Code

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
      ->loadTemplate($template_file)
      ->render($variables);
  } catch (\Twig_Error_Loader $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  } catch (\Twig_Error_Runtime $e) {

    // In case there is a previous exception, re-throw the previous exception,
    // so that the original exception is shown, rather than
    // \Twig_Template::displayWithErrorHandling()'s exception.
    $previous_exception = $e
      ->getPrevious();
    if ($previous_exception) {
      throw $previous_exception;
    }
    throw $e;
  }
  if ($twig_service
    ->isDebug()) {
    $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
    // suggestions are shown first.
    if (!empty($variables['theme_hook_suggestions'])) {
      $variables['theme_hook_suggestions'] = array_reverse($variables['theme_hook_suggestions']);
    }

    // Add debug output for directly called suggestions like
    // '#theme' => 'comment__node__article'.
    if (strpos($variables['theme_hook_original'], '__') !== FALSE) {
      $derived_suggestions[] = $hook = $variables['theme_hook_original'];
      while ($pos = strrpos($hook, '__')) {
        $hook = substr($hook, 0, $pos);
        $derived_suggestions[] = $hook;
      }

      // Get the value of the base hook (last derived suggestion) and append it
      // to the end of all theme suggestions.
      $base_hook = array_pop($derived_suggestions);
      $variables['theme_hook_suggestions'] = array_merge($derived_suggestions, $variables['theme_hook_suggestions']);
      $variables['theme_hook_suggestions'][] = $base_hook;
    }
    if (!empty($variables['theme_hook_suggestions'])) {
      $extension = twig_extension();
      $current_template = basename($template_file);
      $suggestions = $variables['theme_hook_suggestions'];

      // Only add the original theme hook if it wasn't a directly called
      // suggestion.
      if (strpos($variables['theme_hook_original'], '__') === FALSE) {
        $suggestions[] = $variables['theme_hook_original'];
      }
      foreach ($suggestions as &$suggestion) {
        $template = strtr($suggestion, '_', '-') . $extension;
        $prefix = $template == $current_template ? 'x' : '*';
        $suggestion = $prefix . ' ' . $template;
      }
      $output['debug_info'] .= "\n<!-- FILE NAME SUGGESTIONS:\n   " . Html::escape(implode("\n   ", $suggestions)) . "\n-->";
    }
    $output['debug_info'] .= "\n<!-- BEGIN OUTPUT from '" . Html::escape($template_file) . "' -->\n";
    $output['debug_suffix'] .= "\n<!-- END OUTPUT from '" . Html::escape($template_file) . "' -->\n\n";
  }

  // This output has already been rendered and is therefore considered safe.
  return Markup::create(implode('', $output));
}