conbask
conbask

Reputation: 10061

Smarty Conditionals

I am trying to use smarty code to print an opponent's name when one is set, but when the name isn't set, I want to just display "Someone".

{if isset($OPPONENT_FULL_NAME)}
    %%OPPONENT_FULL_NAME%% 
{else} 
    Someone 
{/if} started a game with you.

Right now when I set $OPPONENT_FULL_NAME to something it works well, but when I leave it blank nothing shows up. No "Someone".

What am I doing wrong?

Upvotes: 0

Views: 233

Answers (3)

Lian
Lian

Reputation: 113

I think you should do the filtering, sanitization, or conditioning in php first.

For example:
$OPPONENT_FULL_NAME = (isset($OPPONENT_FULL_NAME) AND !empty($OPPONENT_FULL_NAME)) ? $OPPONENT_FULL_NAME : "Someone";

Then, you can just call {$OPPONENT_FULL_NAME} in smarty template.

However, if you have to do it in smarty template,
{$OPPONENT_FULL_NAME|default: "Someone"} will do the trick.

Hope that help, cheers!

Upvotes: 0

Alex Bustos
Alex Bustos

Reputation: 1

Remember that null is different of empty String

{if !isset($OPPONENT_FULL_NAME) || $OPPONENT_FULL_NAME eq ''}
   {$OPPONENT_FULL_NAME}
{else}
   Someone
{/if|

Upvotes: 0

bhamby
bhamby

Reputation: 15450

The variable $OPPONENT_FULL_NAME may be set, but to a blank string. You might try doing something like

{if isset($OPPONENT_FULL_NAME) && $OPPONENT_FULL_NAME not '' }%%OPPONENT_FULL_NAME%% {else} Someone {/if} started a game with you.

Upvotes: 3

Related Questions