Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Plauralizer

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
boyer
Smarty Rookie


Joined: 08 Apr 2009
Posts: 9

PostPosted: Wed Nov 24, 2010 3:16 am    Post subject: Plauralizer Reply with quote

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
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Wed Nov 24, 2010 9:48 am    Post subject: Reply with quote

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 Wink.
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Fri Nov 26, 2010 5:41 pm    Post subject: plural - plugin Reply with quote

I too wrote a different, but similar plugin here:
http://smarty-framework.googlecode.com/svn/trunk/library/services/framework.sanjaal.com/plugins/modifier.plural.php

It is also incomplete; searching some grammar rules to pluralize.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP