6.x theme.inc theme_render_template($template_file, $variables)
7.x theme.inc theme_render_template($template_file, $variables)

Renders a system default template, which is essentially a PHP template.


$template_file: The filename of the template to render.

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

Return value

The output generated by the template.


includes/theme.inc, line 1518
The theme system, which controls the output of Drupal.


function theme_render_template($template_file, $variables) {

  // Extract the variables to a local namespace
  extract($variables, EXTR_SKIP);

  // Start output buffering

  // Include the template file
  include DRUPAL_ROOT . '/' . $template_file;

  // End buffering and return its contents
  return ob_get_clean();


David Hernández’s picture

I'm just wondering, as I haven't tried it, but maybe is possible to call this function directly with the theme function:

$variables = array('template_file' => 'path/to/template', 'variables' => $template_vars);
theme('render_template', $variables);

If it's possible, it will be possible to render templates without going through all the preprocess and other hooks that you find when you do theme('theme function that uses the template');

Anyways, as I said, this is a theory, as I haven't tried it. And I'm not saying that is a good idea neither, as it breaks every possible way to alter it. It's just interesting :)

msankhala’s picture

I tried with below code but this does not work for me.

  $path_to_theme = drupal_get_path('theme', 'bottle');
  $path_to_module = drupal_get_path('module', 'bottle_custom_forms');
  $variables['overlay_image_path'] = '/' . $path_to_theme . '/images/Interstitial-CVP-Banner.png';
  $variables['overlay_loader_image_path'] = '/' . $path_to_theme . '/images/plane-loader.gif';
  $vars = array(
    'template_file' => $path_to_module . '/templates/bottle-loading-overlay.tpl.php',
    'variables' => $variables,
  $form['loading_dialog'] = array(
    '#type' => 'markup',
    '#markup' => theme('render_template', $vars),

but if i remove theme('render_template', $vars) with theme_render_template($vars['template_file'], $vars['variables']), then it works