模块地址 http://drupal.org/project/blocktheme
安装后,访问配置页面:admin/config/user-interface/blocktheme
如图所示,我添加了三个模板。
那么就要去主题下添加对应的模板文件:
另外,勾选了"Show Custom Block Theme"后,应用了自定义模板的区块会显示它当前应用的模板名。
这些做好了以后,我们就可以使用了,去编辑一个区块,就会看到有模板选择的表单:
如你所见,除了可以选择模板,还可以对这个区块的模板声明自定义的变量。
当然,如果要实现定制区块的模板,除了使用这个区块,我们还可以直接根据模板命名规则去添加模板,命名规则参照block.module文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
function template_preprocess_block(&$variables) {
$block_counter = &drupal_static(__FUNCTION__, array());
$variables['block'] = $variables['elements']['#block'];
// All blocks get an independent counter for each region.
if (!isset($block_counter[$variables['block']->region])) {
$block_counter[$variables['block']->region] = 1;
}
// Same with zebra striping.
$variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';
$variables['block_id'] = $block_counter[$variables['block']->region]++;
// Create the $content variable that templates expect.
$variables['content'] = $variables['elements']['#children'];
$variables['classes_array'][] = drupal_html_class('block-' . $variables['block']->module);
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;
// Hyphens (-) and underscores (_) play a special role in theme suggestions.
// Theme suggestions should only contain underscores, because within
// drupal_find_theme_templates(), underscores are converted to hyphens to
// match template file names, and then converted back to underscores to match
// pre-processing and other function names. So if your theme suggestion
// contains a hyphen, it will end up as an underscore after this conversion,
// and your function names won't be recognized. So, we need to convert
// hyphens to underscores in block deltas for the theme suggestions.
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . strtr($variables['block']->delta, '-', '_');
// Create a valid HTML ID and make sure it is unique.
$variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->module . '-' . $variables['block']->delta);
}
|
只要按照‘theme_hook_suggestions’的规则命名即可。
第二种如果你熟悉命名规则的话,似乎更简单方便,而且少用一个模块,如果你只有少量的区块需要自定义模板,那么直接命名模板是最合适的方案。
但是如果量大,Blocktheme就是提供了一个灵活又易于维护的图形化操作界面,是我比较推荐的方案。