|
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 |
kenzor Smarty Rookie
Joined: 17 Feb 2004 Posts: 9
|
Posted: Thu Feb 09, 2006 2:46 am Post subject: Assign Value Based on Condition |
|
|
I was sick of doing:
Code: |
{{if $myvar > 5}}
{{assign var=somevar value=someval}}
{{else}}
{{assign var=somevar value=someotherval}}
{{/if}}
|
so I created the assignc function.
Usage: {{assignc var=somevar param1=$myvar param2=5 cond=gt}}
where var is what you wish to assign the result to, param1 and param2 are the values you wish to compare, and cond is the condition you wish to compare them on.
Valid conditions are: eq, eeq, gt, lt, gte, lte, ne.
Code: |
function smarty_function_assignc ($_params, &$smarty)
{
if (!isset($_params['var'])) {
$smarty->trigger_error("assign: missing 'var' parameter", E_USER_ERROR, __FILE__, __LINE__);
return;
}
if (!isset($_params['param1'])) {
$_params['param1'] = false;
}
if (!isset($_params['param2'])) {
$_params['param2'] = false;
}
if (!isset($_params['cond'])) {
$smarty->trigger_error("assign: missing 'cond' parameter", E_USER_ERROR, __FILE__, __LINE__);
return;
}
$result = null;
switch ($_params['cond']) {
case 'eq':
$result = ($_params['param1'] == $_params['param2']);
break;
case 'eeq':
$result = ($_params['param1'] === $_params['param2']);
break;
case 'gt':
$result = ($_params['param1'] > $_params['param2']);
break;
case 'gte':
$result = ($_params['param1'] >= $_params['param2']);
break;
case 'lt':
$result = ($_params['param1'] < $_params['param2']);
break;
case 'lte':
$result = ($_params['param1'] <= $_params['param2']);
break;
case 'ne':
$result = ($_params['param1'] != $_params['param2']);
break;
}
$smarty->assign($_params['var'], $result);
}
|
|
|
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
|