|
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 |
clone46 Smarty Rookie
Joined: 13 Jan 2006 Posts: 15
|
Posted: Thu Jun 19, 2014 9:04 pm Post subject: My contribution, a "range" function |
|
|
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 |
|
|
|
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
|