Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

My contribution, a "range" function

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
clone46
Smarty Rookie


Joined: 13 Jan 2006
Posts: 15

PostPosted: Thu Jun 19, 2014 9:04 pm    Post subject: My contribution, a "range" function Reply with quote

Hi!

Here's a helpful function used in situations where you two values and need output like:

1 - 2 dogs
1 dog
4-5 cats

Often times this is a little bit trickier than it sounds.

Example usage:

Code:

{if $HOME.BedsLow or $HOME.BedsHigh}
  {range low=$HOME.BedsLow high=$HOME.BedsHigh delimiter=' - ' singular='bedroom' plural='bedrooms'}
{/if}


Here's the function:

Code:

function smarty_function_range($params, &$smarty)
{
  $low = $params['low'];
  $high = $params['high'];
  $delimiter = (isset($params['delimiter'])) ? $params['delimiter'] : '-';
  $singular = (isset($params['singular'])) ? $params['singular'] : '';
  $plural = (isset($params['plural'])) ? $params['plural'] : '';
 
    if ($low == $high)
    {
        if ($singular != '' && $low == 1) return($low . ' ' . $singular);
        if ($plural != '' && $low != 1) return($low . ' ' . $plural);
        return($low);
    }
    elseif ($low == '0' || $low == '')
    {
        if ($singular != '' && $high == 1) return($high . ' ' . $singular);
        if ($plural != '' && $high != 1) return($high . ' ' . $plural);
        return($high);
    }
    elseif ($high == '0' || $high == '')
    {
        if ($singular != '' && $low == 1) return($low . ' ' . $singular);
        if ($plural != '' && $low != 1) return($low . ' ' . $plural);
        return($low);
    }
    else
    {
        if ($plural != '') return($low . $delimiter . $high . ' ' . $plural);
        return($low . $delimiter . $high);
    }
 
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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