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.
$template_file: The file name of the template to render.
$variables: A keyed array of variables that will appear in the output.
The output generated by the template, plus any debug information.
function twig_render_template($template_file, $variables) {
$variables['_references'] = array();
$output = array(
'debug_prefix' => '',
'debug_info' => '',
'rendered_markup' => drupal_container()
->get('twig')
->loadTemplate($template_file)
->render($variables),
'debug_suffix' => '',
);
if (settings()
->get('twig_debug', FALSE)) {
$output['debug_prefix'] .= "\n\n<!-- THEME DEBUG -->";
$output['debug_prefix'] .= "\n<!-- CALL: theme('{$variables['theme_hook_original']}') -->";
if (!empty($variables['theme_hook_suggestions'])) {
$extension = twig_extension();
$current_template = basename($template_file);
$suggestions = $variables['theme_hook_suggestions'];
$suggestions[] = $variables['theme_hook_original'];
foreach ($suggestions as $key => &$suggestion) {
$template = strtr($suggestion, '_', '-') . $extension;
$prefix = $template == $current_template ? 'x' : '*';
$suggestion = $prefix . ' ' . $template;
}
$output['debug_info'] .= "\n<!-- FILE NAME SUGGESTIONS:\n " . implode("\n ", $suggestions) . "\n-->";
}
$output['debug_info'] .= "\n<!-- BEGIN OUTPUT from '{$template_file}' -->\n";
$output['debug_suffix'] .= "\n<!-- END OUTPUT from '{$template_file}' -->\n\n";
}
return implode('', $output);
}