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

selecting variable to print based on content

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


Joined: 12 May 2003
Posts: 5

PostPosted: Thu Jun 30, 2005 9:03 pm    Post subject: selecting variable to print based on content Reply with quote

Hi,
I'm wondering if somebody can help me implement the following in smarty:

I have two variables $A and $B. Now I want smarty to output the variable $A if $B is *not* set and $B if $B *is* set. Here is some code to illustrate this:

Code:

// notice the use of isset() instead of empty() !

if( isset($B) ) { // return $B even if set but empty (e.g. "")
   return $B;
} else {
  return $A;
}


I guess the thing coming closest would be {$A|default:$B} however this will not work like the code above if $B is merely empty. $B should be selected even if it is empty (but set, like "" for example).
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jun 30, 2005 9:12 pm    Post subject: Reply with quote

Code:
{if isset($B)}$B{else}{$A}{/if}


or write your own default-like |if_isset-modifier, that checks for isset() (or is_null()) instead of checking for empty() as smarty's |default does.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
marvin
Smarty Rookie


Joined: 12 May 2003
Posts: 5

PostPosted: Fri Jul 01, 2005 10:00 am    Post subject: Reply with quote

I'm really aiming to write a modifier since I want to use this in a form like this:
Code:

<input type="text" value="sysname" value="{$SYSTEM.name|if_isset:"sysname"}">


The idea is that if the user makes a mistake when filling out the form I simply redisplay the form. If the user hasn't edited the field at all then I want to use $SYSTEM.name but if he has then I want to pull that value from the smarty variable $ARGS.sysname instead. $ARGS has been initialized with the contents of $_REQUEST (this is a form of abstraction since you should be able to call this code outside of a web context, i.e. in a shell script).

The problem with a syntax like {$A|if_isset:$B} is that $B will get assigned to the first parameter in the modifier even if it isn't set so I cannot check the first parameter in the modifier anymore since it will always be set.

I guess what I'm really asking for is a way to access smarty variables from within a modifier since modifiers don't get provided with access to the smarty object like functions (through &$smarty). That way I could call an isset() directly on the variable stored in smarty.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jul 01, 2005 10:07 am    Post subject: Reply with quote

marvin wrote:
I guess what I'm really asking for is a way to access smarty variables from within a modifier since modifiers don't get provided with access to the smarty object like functions (through &$smarty). That way I could call an isset() directly on the variable stored in smarty.


you don't need that in you modifier. just check for isset() or is_null().
if $B is not assigned and passed to the modifier then it is null inside the modifier.

php has problems to distinguish between variables defined as null and undefined variables. but that is not problem for you here. you can distinguish "" and null inside your modifier. that's all you need.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
marvin
Smarty Rookie


Joined: 12 May 2003
Posts: 5

PostPosted: Fri Jul 01, 2005 12:50 pm    Post subject: Reply with quote

Hm, I tried that before my last reply and it didn't work but after trying again it does. Must have been a typo.

Thanks for the help. This makes my form handling a lot easier.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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