块/主题不“返回”$output
这就是问题所在。
我有一个名为“用户收藏夹块”的块
它位于“第一个侧边栏”区域
“第一个侧边栏”出现在首页上。
该函数应该从 f25_favorites 表中获取数据并将它们列在块内。现在该表是一个空数组。
当我返回 $output
时,我的 div 或任何内容都不会输出。
当我执行 print($output)
时,所有内容都会显示。
这是我的测试代码,用于显示我的“if”语句返回 true。 http://d.pr/zDph
/*
* f25_favorites_my_favorites theme
*/
function theme_f25_favorites_my_favorites($mypaths) {
dsm($mypaths);
print_r(count($mypaths));
$output .= 'n<div id="f25-favorites">n';
$output .= '<div id="f25-favorites-list">n';
if (count($mypaths) == 0) {
$output .= "No favorites have been added";
print "No favorites have been added";
}
else {
foreach ($mypaths as $indpath) {
$output .= l($indpath->title, $indpath->path, $attributes = array());
}
}
$output .= '</div>n';
$output .= '<div id="f25-favorites-add">n';
$output .= '</div>n';
$output .= 'n</div>n';
return $output;
}
这输出: http://d.pr/Uhrs
注意左上角的 0,这是 'count()' 的输出
以及“if”内文本的打印
所以,这是我的主题:
/*
* f25_favorites_my_favorites theme
*/
function theme_f25_favorites_my_favorites($mypaths) {
/*dsm($mypaths);
print_r(count($mypaths));*/
$output .= '\n<div id="f25-favorites">\n';
$output .= '<div id="f25-favorites-list">\n';
if (count($mypaths) == 0) {
$output .= "No favorites have been added";
}
else {
foreach ($mypaths as $indpath) {
$output .= l($indpath->title, $indpath->path, $attributes = array());
}
}
$output .= '</div>\n';
$output .= '<div id="f25-favorites-add">\n';
$output .= '</div>\n';
$output .= '\n</div>\n';
return $output;
}
它是用 hook_theme() 函数调用的:
/*
* Implentation of hook_theme().
*/
function f25_favorites_theme () {
return array(
'f25_favorites_my_favorites' => array (
'arguments' => array ('mypaths' => array())
),
);
}
这是用 hook_block() 函数调用的:
/*
* Implementation of hook_block().
*
*/
function f25_favorites_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks = array();
$blocks['f25-favorites'] = array(
'info' => t('User Favorites Block'),
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
}
if ($op == 'view') {
switch ($delta) {
case 0:
$mypaths = f25_favorites_user_favorites();
$block = array(
'subject' => t('User Favorites Block'),
'content' => theme_f25_favorites_my_favorites($mypaths)
);
return $block;
};
}
}
值得注意
我的主题是名为“Zen”的主题的“子主题”
Zen 有一个 block.tpl.php ,如下所示: http://d.pr/AaO1
这里是我的模块的完整代码: http://d.pr/cGqc
This is the issue.
I have a block called 'User Favorites Block'
It's placed in the region 'First Sidebar'
'First Sidebar' appears on the front-page.
The function is supposed to grab the data from the f25_favorites table and list them inside the block. Right now the table is an empty array.
When I return $output
, none of my divs or anything are output.
When I do print($output)
, everything is displayed.
This is my test code to show that my 'if' statement is returning true. http://d.pr/zDph
/*
* f25_favorites_my_favorites theme
*/
function theme_f25_favorites_my_favorites($mypaths) {
dsm($mypaths);
print_r(count($mypaths));
$output .= 'n<div id="f25-favorites">n';
$output .= '<div id="f25-favorites-list">n';
if (count($mypaths) == 0) {
$output .= "No favorites have been added";
print "No favorites have been added";
}
else {
foreach ($mypaths as $indpath) {
$output .= l($indpath->title, $indpath->path, $attributes = array());
}
}
$output .= '</div>n';
$output .= '<div id="f25-favorites-add">n';
$output .= '</div>n';
$output .= 'n</div>n';
return $output;
}
This outputs this: http://d.pr/Uhrs
Note the 0 on the top left, that's the output of the 'count()'
And the print of the text within the 'if'
So, this is my theme:
/*
* f25_favorites_my_favorites theme
*/
function theme_f25_favorites_my_favorites($mypaths) {
/*dsm($mypaths);
print_r(count($mypaths));*/
$output .= '\n<div id="f25-favorites">\n';
$output .= '<div id="f25-favorites-list">\n';
if (count($mypaths) == 0) {
$output .= "No favorites have been added";
}
else {
foreach ($mypaths as $indpath) {
$output .= l($indpath->title, $indpath->path, $attributes = array());
}
}
$output .= '</div>\n';
$output .= '<div id="f25-favorites-add">\n';
$output .= '</div>\n';
$output .= '\n</div>\n';
return $output;
}
It's called with this hook_theme() function:
/*
* Implentation of hook_theme().
*/
function f25_favorites_theme () {
return array(
'f25_favorites_my_favorites' => array (
'arguments' => array ('mypaths' => array())
),
);
}
Which is called with this hook_block() function:
/*
* Implementation of hook_block().
*
*/
function f25_favorites_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks = array();
$blocks['f25-favorites'] = array(
'info' => t('User Favorites Block'),
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
}
if ($op == 'view') {
switch ($delta) {
case 0:
$mypaths = f25_favorites_user_favorites();
$block = array(
'subject' => t('User Favorites Block'),
'content' => theme_f25_favorites_my_favorites($mypaths)
);
return $block;
};
}
}
Noteworthy
My theme is a 'Sub-theme' of a theme called 'Zen'
Zen has a block.tpl.php which looks like this: http://d.pr/AaO1
Here is the full code of my module: http://d.pr/cGqc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是与区域相关的问题。尝试切换到 Garland 并将该块添加到普通的旧 Garland 区域,然后查看它是否出现。
如果您在 Garland 中看到它,请确保您的子主题确实定义了“First Sidebar”区域,然后在 tpl 文件中实际打印其变量。
(FWIW 我在 Garland 上尝试了你的代码,它显示了很好的块。)
此外,你可能想将你的函数调用从:更改
为:
...如果你想保持代码灵活(即让 Drupal 调用任何预处理函数并允许其他人或您自己将来覆盖模板的输出)
It could be a region-related problem. Try switching to Garland and add the block to a plain old Garland region, and see if it appears.
If you see it in Garland then make sure your sub-theme really is defining that "First Sidebar" region and then actually printing its variable in the tpl files.
(FWIW I tried your code on Garland and it displays the block fine.)
Also, you might want to change your function call from:
to:
...if you want to keep the code flexible (i.e. have Drupal call any preprocess functions and allow other people, or yourself in the future, to override the template's output)