从 drupal 模块打印主题文件中的变量
Print a variable in theme file from drupal module
这是 vegas.module 文件的代码。它用于从特定文件夹加载图像。
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 中打印它,它不会 return 任何值。有没有办法加载模块的变量
您需要使用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 将是可用的 $backgrounds 数组。
我认为将此代码放入您的主题中 template.php 更正确。最容易查看如何更改节点变量
我的主题名称是自定义的。这是我粘贴到 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);
如果您想在 page.tpl.php 中打印此变量 - 使用 hook_preprocess_page
函数custom_preprocess_page(&$variables),不是节点。
这是 vegas.module 文件的代码。它用于从特定文件夹加载图像。
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 中打印它,它不会 return 任何值。有没有办法加载模块的变量
您需要使用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 将是可用的 $backgrounds 数组。
我认为将此代码放入您的主题中 template.php 更正确。最容易查看如何更改节点变量
我的主题名称是自定义的。这是我粘贴到 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);
如果您想在 page.tpl.php 中打印此变量 - 使用 hook_preprocess_page
函数custom_preprocess_page(&$variables),不是节点。