Posts tagged with “smarty”
Copyright Year Smarty Plugin
I've been seeing a lot of people handling the copyright year quite wrong, even though it is a very simple matter, one that should always be automated since it requires an yearly maintenance otherwise. The copyright year should be handled as it follows:
- display the current year only if the website/blog was created this year
- display the creation year and the current year if the website/blog was created one or more years ago
Pretty simple and a lot easier to automatize than to always remember to change the year when it actually needs changing. And since it is somewhere between the back-end and the front-end I think Smarty (or you templating engine of choice) is the way to go for this one and that is why I built a tiny but quite useful Smarty plugin to do this for you.
You just have to provide the "company name" (or your name, or your projects name, you got the point) and the year that the project was created and it handles the rest. Pretty straight-forward.
Example 1:
{copyright company="George Maicovschi" created="2009"}
will output
All content copyright © 2009 George Maicovschi
Example 2:
{copyright company="George Maicovschi" created="2006"}
will output
All content copyright © 2006-2009 George Maicovschi
In order to use the code just copy and paste it in a file name function.copyright.php that is located in Smarty's plugins directory.
<?php
function smarty_function_copyright($params,&$smarty)
{
//The `company` parameter cannot be empty. Check for it;
if (empty($params['company']))
{
$smarty->_trigger_fatal_error("[copyright] param `company` missing");
return;
}
//The `created` parameter cannot be empty. Check for it;
if (empty($params['created']))
{
$smarty->_trigger_fatal_error("[copyright] param `created` missing");
return;
}
$current_year=date('Y',time());
return "All content copyright © ".($current_year>$params['created']?$params['created'].'-'.$current_year:$current_year)." {$params['company']}";
}
?>
Enjoy!
03:32 PM | 2 Comments | Tags: smarty, php, copyright, year, development, pluginTitle 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