Posts tagged with “language”
Title Case Smarty Plugin
I've been spending quite some time lately documenting for some coming and ongoing projects and there is a common mistake I see in a vast majority of the websites. And that is missing a proper formatting of the article's title, or, as it is described in English grammar books, the use of title case
Basically tile case is an English grammar standard for writing titles for articles, books, newspaper columns, etc. And the rules are not that easy to follow: all the words in a title must begin with a capital letter except articles, prepositions and conjunctions if they are not the first word of the title.Pretty simple, right?
Well, even if it's a pretty basic thing to do a lot of us tend to forget about it. And then we go on rambling about W3C standards but forgetting some of the more basic, communication related ones. That is why I decided to create a little PHP function that would do just that, format the title so it would fit the title case standard. But after a few seconds I realized that this is actually related more to the front-end and thus shouldn't stay in the back-end of an webapp/blog/whatever so that's when making it a smarty plugin became natural. And since Smarty is written in PHP (d'oh!) you can always just copy-paste the function and use it straight from PHP.
Aside from the classical requirements I also implemented some other modifiers to make it a little more useful and adaptable for special cases:
- strict - [ boolean ] - when set to false it allows you to have upper case letters inside the words. The default value is TRUE
- forceLower - [ string ] - can contain a comma separated list of words that you want to force to be lower case for the current instance only
But enough with the talk. Lets see some usage examples!
Example 1 - standard usage
{titlecase title='snow white and the seven dwarfs'}
This example will output: Snow White and the Seven Dwarfs
Example 2 - turning `strict` off
{titlecase title='snow whIte and the seven dwarfs'}
This will output: Snow White and the Seven Dwarfs
but is used like
{titlecase title='snow whIte and the seven dwarfs' strict=false}
it will output: Snow WhIte and the Seven Dwarfs
Example 3 - using `forceLower`
{titlecase title='snow White and the seven dwarfs' forceLower='white,dwarfs'}
In this case the output will be: Snow white and the Seven dwarfs
I hope that these examples clarify how the hole plugin works so it's time for you to get your code :-). In order to use the code just copy and paste it in a file name function.titlecase.php that is located in Smarty's plugins directory.
<?php
function smarty_function_titlecase($params,&$smarty)
{
//The `title` parameter cannot be empty. Trigger a fatal error if it is
if (empty($params['title']))
{
$smarty->_trigger_fatal_error("[titlecase] param `title` missing");
return;
}
//Check if we are running in strict more or not
if (!isset($params['strict'])) $params['strict']=true;
//Add words that you want to always be lower-case only to this array
$force_lower=array('about','above','across','after','against','around','at','before','behind','below',
'beneath','beside','besides','between','beyond','by','down','during','except','for','from','in','inside',
'into','like','near','of','off','on','out','outside','over','since','through','throughout','till','to',
'toward','under','until','up','upon','with','without','according to','because of','by way of',
'in addition to','in front of','in place of','in spite of','instead of','on account of','out of','a',
'an','the','and','or','but');
//Check if there are any instance-only forced lower case words
if (!empty($params['forceLower'])) $force_lower=array_merge($force_lower,explode(',',$params['forceLower']));
//Build the regex patterns for whole-word matching based on the words forced to lower case
foreach ($force_lower as $word) $regex["/(\b({$word})\b)/i"]=$word;
//And finally rebuild the string in title case
return ucfirst(preg_replace(array_keys($regex),array_values($regex),ucwords(($params['strict']?strtolower($params['title']):$params['title']))));
}
?>
Enjoy!
02:43 AM | 0 Comments | Tags: php, smarty, title case, english, language, standards, plugin, development