Smarty and Capitalization Modifier

I do a lot of web design, and I use the Smarty templating engine for all of my PHP websites.  It is a fantastic template system, designed for rapid development, and it succeeds at everything it does.

Well, almost everything.

Smarty has modifiers, which allow you to control data dynamically in your templates.  A lot of people complain that this feature of Smarty puts too much logic in the template system, which by its very nature is supposed to seperate business logic from display logic.  However, some modifiers such as "capitalize" fit under display logic as far as I am concerned.  One limitation of this modifier is that it does not handle contractions very well at all.

Take this for example:

example.tpl:

{assign value="max's horse didn't fall. 'someone' is coming.  they're here!" var="capt"}
{$capt|capitalize}

Output:
Max'S Horse Didn'T Fall. 'Someone' Is Coming. They'Re Here!

As you can see, Smarty treats the apostrophe as a word boundary, resulting in incorrect capitalization.  The way to fix this behavior is to modify [smarty]/libs/plugins/modifier.capitalize.php.

function smarty_modifier_capitalize($string, $uc_digits = false)
{
//replace common contraction endings with placeholders
//that PCRE will not see as a word boundary
$apos = array('n\'t ','\'s ','\'re ');
$tmps = array('n__apos__t ','__apos__s ','__apos__re ');
$string = str_replace($apos,$tmps,$string);
//do the real work
smarty_modifier_capitalize_ucfirst(null, $uc_digits);
$string = preg_replace_callback('!\b\w+\b!', 'smarty_modifier_capitalize_ucfirst', $string);
//restore the contractions and return
return str_replace($tmps,$apos,$string);
}

example.tpl:

{assign value="max's horse didn't fall. 'someone' is coming.  they're here!" var="capt"}
{$capt|capitalize}

Output:
Max's Horse Didn't Fall. 'Someone' Is Coming. They're Here!

[tags]Smarty,PHP,modifier,capitalize,template,display+logic[/tags]

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011