Home | Contact | Search | About

Navigation

Syndicate

cmsInfo

New Feature for the Next Drupal: Alternate Block Templates

Submitted by joe on Sat, 2006-08-19 18:02.

The next version of Drupal is shaping up nicely and the current CVS seems pretty stable.

One nice feature in the upcoming release are template "suggestions". This is an array that gets built and converted into a list of filenames to look for alternate templates. This works for pages as well as blocks.

For a given block the phptemplate engine will look for:

  1. block-modulename-id.tpl.php
  2. block-modulename.tpl.php
  3. block-region.tpl.php
  4. Fall back to block.tpl.php

This is very nice for customizing layout when launching a site.

However what is missing in my mind is a way for customizing a block's appearance from the block admin interface.

Really the only one of these different options above that is available to the block administrator is the region. Adding regions to a page is a nice way to allow the site administrator to control appearance. But all blocks in a region will have the same appearance.

Two of my pet peeves in this regard are advertising blocks and syndication chicklet icon blocks. When the site has titles and borders around these blocks it degrades what may be an otherwise nice site.

This hits me as a developer/consultant in two specific ways.

  1. I do not want to add a template when my site editor needs to add a block without a title or border.
  2. I do not want my client to need to contact me to add a template when all they want to do is add a new block that looks different from others in a sidebar.

As a proof of concept I added a feature that in the block admin interface, the administrator can add a suggestion that will be used as a template name.

For this demonstration I used a hackish method for adding the style suggestion. Entering a string like "{style}" in the block title will cause the template engine to look for a block template file named block-style.tpl.php. The "{style}" will not be displayed on the site.

This is the code:

function phptemplate_block($block) {
  $suggestions[] = 'block';
  $suggestions[] = 'block-' . $block->region;
  $suggestions[] = 'block-' . $block->module;
  // If {text} is in block title
  if( preg_match("/{([^ \t\r\n}]+)}/", $block->subject, $match)) {
    // add template suggestion
    $suggestions[] = 'block-' . $match[1];
    // remove from block title
    $block->subject = preg_replace("/{([^ \t\r\n}]+)}/", "",$block->subject);
  }
  $suggestions[] = 'block-' . $block->module . '-' . $block->delta;

  return _phptemplate_callback('block', array('block' => $block), $suggestions);
}

This simple change puts power into the block administrators hands. The site designer and developer still create any template so only what options they give for each theme is there.

This brings up a couple of discussion points.

  1. Does the Drupal community see the need for the site administrator to have this power?
  2. What would be the best UI and data storage for a block "style" entry like this?
  3. This doesn't address module blocks. Another side issue is that you can't rename a module's block title from the block admin.

Thanks go to the Drupal developers for continuing to extend the flexibility in an elegent manner.

Related Links:

Block admin should have ability to designate appearance "style"
http://drupal.org/node/49775

Commit #77184 by Eaton, look for specific block templates based on region, module, and delta. http://cvs.drupal.org/viewcvs/drupal/drupal/themes/engines/phptemplate/phptemplate.engine?r1=1.39&r2=1.40

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Submitted by joe on Tue, 2006-09-05 14:01.

The side note is now out of date. The block admin will be able to rename a module's block title in the next Drupal release.

http://cvs.drupal.org/viewcvs/drupal/drupal/modules/block/block.module?r1=1.219&r2=1.220


Google
 
Web www.cmsinfo.org

Drupal Feeds