Drupal 7 动态主题
我正在尝试开发一个模块,其输出的主题化方式与 Views 模块非常相似,但我似乎无法让它工作。我遵循使用主题层 (http://drupal.org/node/933976) 并搜索了 drupal 论坛,但没有结果。
主题钩子在 hook_theme 中定义为
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
,我通过 in node--beer.tpl.php
调用主题钩子
print render($element);
(beer 是内容类型名称),其中 $element
是带有 #theme 的渲染数组
array(3) {
[0] => string(19) "beerxml_hop__simcoe"
[1] => string(11) "beerxml_hop"
[2] => string(15) "beerxml_unknown"
}
被调用的模板是 beerxml_hop,而不是我所希望的 beerxml_hop__simcoe
。 beerxml-hop--simcoe.tpl.php
和 beerxml-unknown.tpl.php
都与 beerxml-hop.tpl.php< 位于同一目录中/code> 和
beerxml-unknown.tpl.php
在输出中的其他地方使用。
我缺少什么? :)
I'm trying to develop a module whose output is to be themable in a way very similar to that of the Views module, but I can't seem to get it to work. I've followed Using the Theme Layer (http://drupal.org/node/933976) and searched the drupal forums to no avail.
The theme hook is defined in hook_theme as
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
And I invoke the theme hook by
print render($element);
in node--beer.tpl.php
(beer is the content type name) where $element
is a render array with #theme
array(3) {
[0] => string(19) "beerxml_hop__simcoe"
[1] => string(11) "beerxml_hop"
[2] => string(15) "beerxml_unknown"
}
The template that gets invoked is beerxml_hop
, and not beerxml_hop__simcoe
as I would have hoped. Both beerxml-hop--simcoe.tpl.php
and beerxml-unknown.tpl.php
exists in the same directory as beerxml-hop.tpl.php
and beerxml-unknown.tpl.php
gets used elsewhere in the output.
What am I missing? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Drupal 不会搜索模块文件夹内具有动态部分的模板。您必须使用几行代码手动完成此操作:
但是,此技巧有一些限制:
Drupal is not searching for templates with dynamic part inside of the module folder. You have to do it manually with a few lines of code:
However, this trick has some limitations:
您的模式必须与您的第一个
$element['#theme']
选项匹配您可以尝试
Your pattern must match your first
$element['#theme']
optionYou may try
hook_theme_registry_alter 的实现是解决该问题的关键。
另一件非常重要的事情是避免在模板名称中使用“-”!
例如,这行不通:
关键是(尽管在问题中):
。
然后渲染 beerxml-hop--something.tpl.php 文件应该这样完成:
Implementation of hook_theme_registry_alter was key in solving the issue.
Another thing very important is to avoid using '-' in the template names !
For instance, this won't work:
It is key (as is in the question though) to:
.
Rendering the beerxml-hop--something.tpl.php file should then be done with: