You are here

function hook_render_template in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/theme.api.php \hook_render_template()
  2. 9 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. The helper function theme_render_and_autoescape() may be used for this.

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 775
Hooks and documentation related to the theme and render system.

Code

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