Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By geerlingguy on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.0-alpha14
Issue links:
Description:
In Drupal 7 and below, the PHPTemplate rendering engine allowed you to render arbitrary PHP template files (e.g. mytemplate.tpl.php) to a string of markup using the theme_render_template()
function. This function is useful when generating markup programmatically or templating files which aren't meant to be rendered to a full HTML page response.
In Drupal 8, when the default theme system was changed to Twig, the theme_render_template()
function was replaced by twig_render_template()
, which substantially does the same thing.
Before (Drupal 7 and below):
$output = theme_render_template(drupal_get_path('module', 'my_module') . '/templates/my-template.tpl.php', array(
'my-variable' => 'value',
));
After (Drupal 8):
$markup = twig_render_template(drupal_get_path('module', 'my_module') . '/templates/my-template.html.twig', array(
'my-variable' => 'value',
));
$output = (string) $markup;
Impacts:
Module developers
Themers