Solving the widow problem on your Drupal website

Posted by andu
Sun, 2007-08-05 12:21

What is the widow problem you might ask?

in typesetting, a widow is a single word on a line by itself at the end of a paragraph and is considered bad style.
widow description

From Shaun Inman.

I'm going to present two methods of avoiding the widow problem on your website, each with it's own advantages and disadvantages.

  1. The client side approach

    Using Javascript we can manipulate the headings after the page has loaded and add a non breaking space between the last two words of the content. There's a jQuery plugin which does this. I modified it to solve some bugs and created a module which uses it. It applies the transformations to elements which match one of the following rules:

    • #content h1.title
    • #content h2.node-title a

    It's available here: http://drupal.org/project/widont

    Advantages: easy to install, most of the time works out of the box.

    Disadvantages: it doesn't work when Javascript is disabled, on some themes who use different classes for the title headings it will fail.

  2. The server side approach

    You'll need to know how to edit the files from the theme you're using. I'm going to assume that you're using Garland, the default Drupal theme, but the instructions can be applied to every Drupal theme out there.

    First you'll have to edit the template.php file which is located in your theme directory. Add the following code there:

    1. function garland_widont($str = '')
    2. {
    3. $str = rtrim($str);
    4. $space = strrpos($str, ' ');
    5. if ($space !== false)
    6. {
    7. $str = substr($str, 0, $space).' '.substr($str, $space + 1);
    8. }
    9. return $str;
    10. }

    Replace garland with your theme's name. E.g. k2_widont, bluemarine_widont.

    Now we need to edit the files where the titles are printed. First, page.tpl.php. Find the following code and replace it with the one beneath it.

    1. <?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>

    1. <?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. garland_widont($title) .'</h2>'; endif; ?>

    Next, node.tpl.php:

    Find

    1. <?php if ($page == 0): ?>
    2. <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
    3. <?php endif; ?>

    and replace it with:
    1. <?php if ($page == 0): ?>
    2. <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print garland_widont($title) ?></a></h2>
    3. <?php endif; ?>

    If you have separate template files for each node type (e.g. node-page.tpl.php and node-story.tpl.php) you'll need to edit those as well. Also, you can edit the block template file (block.tpl.php) or the comments one (comment.tpl.php) if you want.

    Advantages: works even if Javascript is turned off, works on all themes.

    Disadvantages: you have to do it everytime you change your theme, it's harder to implement.

Trackback URL for this post:

http://voidberg.org/trackback/224

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous (not verified) - Tue, 2007-08-28 21:32

With regards to accessibility. If a user grows/shrinks their text, due to floating wont this always be an issue no matter what?

If thats the case, and at some point you will get:
The
lonely
life
of
an
unlucky
widow

why bother trying to fix it. The web is not a physical medium, printers and screen will all treat the text differently. The php approach is costly because its a cost served up on every page (even with caching, it must be done once at some time). The JS solution is not guarenteeable (not everyone can run js, whereas php is always on the server).

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.