是否可以将变量追溯回其extract()函数?

时间:2012-04-05 19:54:56

标签: php drupal

我正在使用Drupal主题,我看到很多变量看起来像是用extract()创建的。是否可以追溯,并查看该阵列的位置?

3 个答案:

答案 0 :(得分:2)

我指的是传递给模板文件的变量,这些变量实际上是从数组中提取的。

Drupal 7中的代码位于theme_render_template()

function theme_render_template($template_file, $variables) {
  extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
  ob_start(); // Start output buffering
  include DRUPAL_ROOT . '/' . $template_file; // Include the template file
  return ob_get_clean(); // End buffering and return its contents
}

该函数从theme()调用,执行以下代码。

// Render the output using the template file.
$template_file = $info['template'] . $extension;
if (isset($info['path'])) {
  $template_file = $info['path'] . '/' . $template_file;
}
$output = $render_function($template_file, $variables);
默认情况下,

$render_function设置为'theme_render_template',但其值使用以下代码设置(在theme()中)。

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

答案 1 :(得分:1)

只需回显$GLOBALS变量,如果数组未设置,您可能会发现它来自哪里。

答案 2 :(得分:0)

我不熟悉Drupal所以这只是一个建议,但是如果drupal有一个模板结构,或者如果从控制器传递一个数组,那么可能使用该提取, 您可以在视图中使用get_defined_vars来获取所有变量,并且可能存在一个数组,您可以使用您知道的变量与同一数组中的变量进行交叉引用。

<?php 
$vars = get_defined_vars();
print_r($vars);
//or maybe
print_r($this);
?>
相关问题