2015-10-08 31 views
1

这是vegas.module文件的代码。它用于从特定文件夹加载图像。从drupal模块打印主题文件中的变量

function vegas_init() { 
    // Load all the images to be added to Vegas. 
    $backgrounds = array(); 
    $fade = variable_get('vegas_fade', 0); 
    for ($i = 0; $i < 10; $i++) { 
    $fid = variable_get('vegas_images_' . $i, ''); 
    if (!empty($fid)) { 
     $image = file_load($fid); 
     if ($image) { 
     $background = array(
      'src' => file_create_url($image->uri), 
     ); 
     if (!empty($fade)) { 
      $background['fade'] = intval($fade); 
     } 
     $backgrounds[] = $background; 
     } 
    } 
    } 

我将它打印在.module文件中。它给出了预期的结果。

print_r($backgrounds); 

如果我打印它在我的主题的page.tpl.php它不会返回任何值。是否有任何方法加载模块的变量

回答

1

如果你想打印varibale在page.tpl.php中 - 使用hook_preprocess_page

function custom_preprocess_page(& $ variables),not node。

+0

为什么我应该把这个函数放在template.php –

+0

如果有些开发者在你改变你的模板文件之后 - 他会在一个文件中看到模板变量的所有变化,他不会在模块中搜索这个变化。 – DrHolera

+0

谢谢DrHolera博士 –

0

您需要使用hook_preprocess_page将变量添加到页面模板或hook_preprocess_node以将变量添加到节点模板。

https://api.drupal.org/api/drupal/modules!node!node.module/function/template_preprocess_node/7

function MYMODULE_preprocess_node(&$variables) { //can be MYTHEME_preprocess_node and locate in template.php 
    // Load all the images to be added to Vegas. 
    $backgrounds = array(); 
    $fade = variable_get('vegas_fade', 0); 
    for ($i = 0; $i < 10; $i++) { 
    $fid = variable_get('vegas_images_' . $i, ''); 
    if (!empty($fid)) { 
     $image = file_load($fid); 
     if ($image) { 
     $background = array(
      'src' => file_create_url($image->uri), 
     ); 
     if (!empty($fade)) { 
      $background['fade'] = intval($fade); 
     } 
     $variables['backgrounds'][] = $background; 
     } 
    } 
    } 

试试这个代码和yoot node.tpl.php将avaliable $背景阵列。

我觉得更正确的把这个代码放到template.php文件中。这将是最容易查看如何chaged节点变量

0

我的主题名称是自定义的。这是我已经粘贴到template.php文件中

function custom_preprocess_node(&$variables) { //can be MYTHEME_preprocess_node and locate in template.php 
    // Load all the images to be added to Vegas. 
    $backgrounds = array(); 
    $fade = variable_get('vegas_fade', 0); 
    for ($i = 0; $i < 10; $i++) { 
    $fid = variable_get('vegas_images_' . $i, ''); 
    if (!empty($fid)) { 
     $image = file_load($fid); 
     if ($image) { 
     $background = array(
      'src' => file_create_url($image->uri), 
     ); 
     if (!empty($fade)) { 
      $background['fade'] = intval($fade); 
     } 
     $variables['backgrounds'][] = $background; 
     } 
    } 
    } 
} 

,并打印文件page.tpl.php中

print_r($backgrounds);