A quick Smarty plugin for eliminating widows – based on code shared by Shaun Inman in his post WIDON’T
So what’s it for? Well to quote Shaun’s fine explanation:
For those who don’t know, in typesetting, a widow is a single word on a line by itself at the end of a paragraph and is considered bad style.
...most people never even notice when it occurs in body copy. But when you’re dealing with a short headline, common on blogs and news sites, the errant word can be quite ugly.
The solution is to insert a non-breaking space between the last two words of your headline.
The code
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: modifier.widont.php * Type: modifier * Name: widont * Date: Oct 20, 2007 * Purpose: convert last space to nonbreaking space ( ) * Example: {$yourTextVariable|widont} * ------------------------------------------------------------- */function smarty_modifier_widont($str = '') { $str = rtrim($str); $space = strrpos($str, ' '); if ($space !== false) { $str = substr($str, 0, $space).' '.substr($str, $space + 1); } return $str; }?>
Installation & usage
Copy the code above into a plaintext document and save it as ‘modifier.widont.php’ in the plugins directory in your Smarty installation and call it in the normal Smarty modifier way on any variable you want to eliminate widows on:
{$yourTextVariable|widont}
An alternative!
Markus Kniebes has already made an alternative (regular expression) version which you can find at deep-resonance.org/journal/widont.
More
If you are looking for other implementations of this idea, Shaun’s original post was in fact a Wordpress plugin for eliminating widows and if you scroll down to the section ‘Updates’ there are links to Movable Type, Textpattern, Expression Engine & jQuery versions.
In January 2007 Shaun updated his Wordpress plugin to use a regular expression – you can find that post & download at WIDON’T 2.1.
0 Comments