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

Short cuts for 'checked' and 'selected' and booleans

 
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 -> General
View previous topic :: View next topic  
Author Message
cablehead
Smarty Rookie


Joined: 09 Jul 2003
Posts: 23

PostPosted: Thu Sep 18, 2003 3:18 am    Post subject: Short cuts for 'checked' and 'selected' and booleans Reply with quote

I've been trying to think of clean shortcuts to handle generating selected and checked phrases + handle simple displaying of text pivoted around a boolean.

For example:

Code:

<input type=checkbox name=fred value=3 {if $selected eq 3}checked{/if}>
<option value=3 {if $selected eq 3}selected{/if}>
$name {if $access}has access.{else}does not have access.{/if}


Here's my best thought so far:

Code:

<input type=checkbox name=fred value=3 {$selected|asCheckedIf:3}>
<option value=3 {$selected|asSelectedIf:3>
$name {$access|asEither:has access.:does not have access.}


They don't feel entirely comfortable though. Just hoping to bounce the idea of others to see if a more natural way can (has) been found.

Here's the code for the modifiers .. not exactly rocket science Smile But to save people typing if interested:

Code:

#
# Modifier: asEither - shows a boolean as either of the two supplied values
#                      depending on whether the boolean is true or false
#
function smarty_modifier_asEither($ysnCond, $strTruePhrase, $strFalsePhrase="") {
   if($ysnCond) return $strTruePhrase;

   return $strFalsePhrase;
}

#
# Modifier: asCheckedIf - shows checked if modified variable equals supplied variable
#
function smarty_modifer_asCheckedIf( $mixSelected, $mixValue ) {
   if( $mixSelected == $mixValue ) return " checked";
   else return "";
}

#
# Modifier: asSelectedIf - shows selected if modified variable equals supplied variable
#
function smarty_modifer_asSelectedIf( $mixSelected, $mixValue ) {
   if( $mixSelected == $mixValue ) return " selected";
   else return "";
}

$smarty->register_modifier( "asEither", "smarty_modifier_asEither" );
$smarty->register_modifier( "asCheckedIf", "smarty_modifier_asCheckedIf" );
$smarty->register_modifier( "asSelectedIf", "smarty_modifier_asSelectedIf" );                        
Back to top
View user's profile Send private message Send e-mail
cablehead
Smarty Rookie


Joined: 09 Jul 2003
Posts: 23

PostPosted: Thu Sep 18, 2003 3:38 am    Post subject: Reply with quote

Sorry

smarty_modifer_asCheckedIf
smarty_modifer_asSelectedIf

should be

smarty_modifier_asCheckedIf
smarty_modifier_asSelectedIf

Sad
Back to top
View user's profile Send private message Send e-mail
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 -> General 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