[英]Drupal 7 Dynamic Theming
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. 我正在尝试开发一个模块,该模块的输出将以与Views模块非常相似的方式可操作,但是我似乎无法使其正常工作。 I've followed Using the Theme Layer (http://drupal.org/node/933976) and searched the drupal forums to no avail.
我已经关注了使用主题层 (http://drupal.org/node/933976),并在drupal论坛上进行了搜索,但没有成功。
The theme hook is defined in hook_theme as 在hook_theme中将主题挂钩定义为
'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 在
node--beer.tpl.php
(啤酒是内容类型名称)中,其中$element
是带有#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. 被调用的模板是
beerxml_hop
,而不是我希望的beerxml_hop__simcoe
。 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. beerxml-hop--simcoe.tpl.php
和beerxml-unknown.tpl.php
存在于同一目录中,而beerxml-hop.tpl.php
和beerxml-unknown.tpl.php
在输出中的其他地方都可以使用。
What am I missing? 我想念什么? :)
:)
Drupal is not searching for templates with dynamic part inside of the module folder. Drupal不在模块文件夹内搜索具有动态部分的模板。 You have to do it manually with a few lines of code:
您必须使用以下几行代码手动进行操作:
/**
* Implements hook_theme_registry_alter().
*/
function MY_MODULE_theme_registry_alter(&$registry) {
$path = drupal_get_path('module', 'MY_MODULE') . '/subfolder/with/templates';
$registry += drupal_find_theme_templates($registry, '.tpl.php', $path);
}
However, this trick has some limitations: 但是,此技巧有一些局限性:
Implementation of hook_theme_registry_alter was key in solving the issue. hook_theme_registry_alter的实现是解决该问题的关键。
Another thing very important is to avoid using '-' in the template names ! 另一个非常重要的事情是避免在模板名称中使用“-”!
For instance, this won't work: 例如,这将不起作用:
'beerxml-hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml-hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
It is key (as is in the question though) to: 这是关键(尽管在问题中如此):
. 。
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
Rendering the beerxml-hop--something.tpl.php file should then be done with: 然后,应使用以下步骤来渲染beerxml-hop--something.tpl.php文件:
echo theme('beerxml-hop--something', array('n' => 10));
Your pattern must match your first $element['#theme']
option 您的模式必须与您的第一个
$element['#theme']
选项匹配
You may try 你可以试试
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__[a-z]+',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.