|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
boyer Smarty Rookie
Joined: 08 Apr 2009 Posts: 9
|
Posted: Wed Nov 24, 2010 3:16 am Post subject: Plauralizer |
|
|
Hey all,
I apologize if there is already a similar plugin posted here, but thought I'd share mine anyway just in case.
How many times do you end up doing this in your site?
Code: | You have {$numberOfSomething} thing{if $numberOfSomething != 1}s{/if} |
I know I do that all the time when displaying the number of blog entries, messages, or whatever. It's really lame to just put something(s). I always do the logic, but wanted a cleaner way. Hello plugin!
The plugin I wrote is a modifier, called "plauralize". It works like this:
Code: | You have {$numberOfSomething} thing{$numberOfSomething|plauralize} |
This will simply output an "s" if the number is not one. Otherwise, it returns an empty string.
Code: | <?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty plugin
*
* Type: modifier<br>
* Name: plauralizer<br>
* Date: Nov 23 2010
* Purpose: Add an "s" at the end of a word if the number isn't 1
* Example: {$number|plauralize}
* @version 1.0
* @author Sean Boyer <sean@boyercentral.net>
* @param int
* @return string
*/
function smarty_modifier_plauralize($int)
{
if(intval($int) != 1)
return "s";
return "";
}
/* vim: set expandtab: */
?>
|
Hope that someone finds this useful!
Ciao! |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Wed Nov 24, 2010 9:48 am Post subject: |
|
|
Thanks for sharing it.
An advanced option could be to support individual chars - not every language use the char "s" as flag for more than one thing . |
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|