function hook_render_template

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_render_template()
  2. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_render_template()
  3. 10 core/lib/Drupal/Core/Render/theme.api.php \hook_render_template()

Render a template using the theme engine.

It is the theme engine's responsibility to escape variables. The only exception is if a variable implements \Drupal\Component\Render\MarkupInterface. Drupal is inherently unsafe if other variables are not escaped.

Parameters

string $template_file: The path (relative to the Drupal root directory) to the template to be rendered including its extension in the format 'path/to/TEMPLATE_NAME.EXT'.

array $variables: A keyed array of variables that are available for composing the output. The theme engine is responsible for passing all the variables to the template. Depending on the code in the template, all or just a subset of the variables might be used in the template.

Return value

string The output generated from the template. In most cases this will be a string containing HTML markup.

Related topics

2 functions implement hook_render_template()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

nyan_cat_render_template in core/modules/system/tests/themes/engines/nyan_cat/nyan_cat.engine
Implements hook_render_template().
twig_render_template in core/themes/engines/twig/twig.engine
Implements hook_render_template().

File

core/lib/Drupal/Core/Render/theme.api.php, line 832

Code

function hook_render_template($template_file, $variables) {
    $twig_service = \Drupal::service('twig');
    return $twig_service->loadTemplate($template_file)
        ->render($variables);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.