Drupal从主题模板中的模块添加渲染元素
我创建了一个模块,它返回一点文本,目的是可以在主题模板文件中呈现。看起来这应该是直截了当的,但我在某些事情上被绊倒了。到目前为止,我一直在尝试,并在我的模块中创建了以下函数:Drupal从主题模板中的模块添加渲染元素,drupal,drupal-7,drupal-theming,Drupal,Drupal 7,Drupal Theming,我创建了一个模块,它返回一点文本,目的是可以在主题模板文件中呈现。看起来这应该是直截了当的,但我在某些事情上被绊倒了。到目前为止,我一直在尝试,并在我的模块中创建了以下函数: function daily_hours_theme() { $items = array( 'daily_hours_text_content' => array( 'render element' => 'element', ) ); return $items;
function daily_hours_theme() {
$items = array(
'daily_hours_text_content' => array(
'render element' => 'element',
)
);
return $items;
}
function theme_daily_hours_text_content($vars) {
return daily_hours_get_text();
}
function daily_hours_text_content_render() {
$build = array(
'#theme' => 'daily_hours_text_content',
'#module' => 'daily_hours',
'content' => daily_hours_get_text(),
);
return render($build);
}
function daily_hours_get_text($day = null) {
// Logic to get correct text content and store in $hours
return $hours;
}
我一直在尝试在我的主题文件中使用以下内容呈现文本:
print drupal\u render($daily\u hours\u text\u content\u render)代码>和
print drupal\u render($daily\u hours\u text\u content)代码>
但是一点运气都没有。如果我直接调用print theme('daily\u hours\u text\u content')
,文本就会呈现。如果我在调用print drupal\u render($render\u array)之前在模板文件中提供以下数组,它也会进行渲染代码>-
$render_array = array(
'#theme' => 'daily_hours_text_content'
);
因此,似乎主题函数正在工作,但渲染没有工作,或者我没有在模板文件中正确使用drupal\u render
函数。有人能告诉我这里缺少什么吗?你的主题\每日\小时\文本\内容
函数应该返回html,而不是打印它,就像你链接到的帖子中的主题\内容
函数那样。如果要使用模板文件,最好使用变量
数组键,而不是渲染元素
,并设置模板
数组键。查看文档,我的主题\每日\小时\文本\内容
正在返回文本,而不是打印文本。我仅在尝试在模板文件中渲染时调用print
。我真的不想为此使用模板文件-我想使渲染数组可用于已在使用的主题模板文件。?要使用已注册的主题函数或文件,您只需使用适当的参数调用,无需注册自己的ThemeEyes,当我直接调用主题
函数时,它就起作用了,但当我尝试使用drupal_render
时,它就不起作用了,您链接到的主题函数文档中建议了这一点。