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]
Recent comments
3 weeks 5 days ago
8 weeks 1 day ago
8 weeks 1 day ago
2 years 6 days ago
2 years 1 week ago
2 years 22 weeks ago
2 years 22 weeks ago
2 years 29 weeks ago
2 years 39 weeks ago
2 years 11 weeks ago