I haven't quite figured out how Drupal passes the values from the database or the back-end to the theme variables in the template.

Which function does it use to disperse the variables to the corresponding template?

  • So, you probably already checked the docs and skimmed a little bit through the core code. What exactly is unclear about that? Please update your question and make it more specific. Maybe pick a more specific example you want to know more about. And tell us what you've tried to solve the mystery yourself so far. Otherwise this question unfortunately is way too broad. – leymannx Jan 15 '19 at 12:39
  • It uses extract() – Clive Jan 15 '19 at 12:56

The function that does the job is theme_render_template(), which contains the following code.

  // 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();

The function is called from theme(), which uses the following code.

// Default render function and extension.
$render_function = 'theme_render_template';
$extension = '.tpl.php';

// The theme engine may use a different extension and a different renderer.
global $theme_engine;
if (isset($theme_engine)) {
  if ($info['type'] != 'module') {
    if (function_exists($theme_engine . '_render_template')) {
      $render_function = $theme_engine . '_render_template';
    $extension_function = $theme_engine . '_extension';
    if (function_exists($extension_function)) {
      $extension = $extension_function();

// In some cases, a template implementation may not have had
// template_preprocess() run (for example, if the default implementation is
// a function, but a template overrides that default implementation). In
// these cases, a template should still be able to expect to have access to
// the variables provided by template_preprocess(), so we add them here if
// they don't already exist. We don't want to run template_preprocess()
// twice (it would be inefficient and mess up zebra striping), so we use the
// 'directory' variable to determine if it has already run, which while not
// completely intuitive, is reasonably safe, and allows us to save on the
// overhead of adding some new variable to track that.
if (!isset($variables['directory'])) {
  $default_template_variables = array();
  template_preprocess($default_template_variables, $hook);
  $variables += $default_template_variables;

// Render the output using the template file.
$template_file = $info['template'] . $extension;
if (isset($info['path'])) {
  $template_file = $info['path'] . '/' . $template_file;
if (variable_get('theme_debug', FALSE)) {
  $output = _theme_render_template_debug($render_function, $template_file, $variables, $extension);
else {
  $output = $render_function($template_file, $variables);
  • Just what I was looking for. Thanks for the info mate!!!! – Binny Jan 17 '19 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.