Are blocks section or div, an HTML5 conumdrum

This debate is going to rear its head in the Drupal community sooner rather than later because right now we're seriously thinking about HTML5 for Drupal 8, and because many new HTML5 themes are springing up.

The issue is that <section> elements must have a heading, that is an <h*> element. If they don't have a heading then technically they're not a section. This is problematic for Drupal blocks because headings are optional, even end users can toggle the display of the block heading.

In Adaptivetheme for Drupal 7, which is HTML5, I decided to solve this thorny issue by using conditional elements. Its pretty simple - if the block has a heading it's uses <section> elements, if there's no heading we use a <div>.

<?php $tag = $block->subject ? 'section' : 'div'; ?>
<<?php print $tag; ?>>
  ...other block stuff here...
</<?php print $tag; ?>>

This logic is very simple and is coded directly into the block template. Perhaps we could actually use something like theme_block() to provide this logic and build the templates more like Views views-view-fields.tpl.php, in which case perhaps we end up with something more like this in the block template:

<?php print $block->wrapper_prefix; ?>
  ...other block stuff here...
<?php print $block->wrapper_suffix; ?>

This automagic markup somewhat irks me, however I can't, at time of writing, see a better way of doing this. Clearly blocks can be secion, div, or even aside, so we need a better way of generating block markup.

Blocks are not alone in this conundrum, there are quite a few places where this would be applicable, not least of which is fields, which I will cover in my next installment on HTML5.

Last updated 4th January, 2011 - 10:10pm

Authored by Jeff Burnz on