Re:Question about handling undefined variables
| From: | Dan Thompson | Date: | Sat, 12 Jan 2002 00:07:47 +0000 |
| Subject: | Re:Question about handling undefined variables | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2524@lists.php.net to get a copy of this message | ||
Thanks for all the help.
I tried as suggested using
{if isset($description)} {$description} {/if}
which worked as needed.
I then modified it to
{if isset($description)} {$description} {else} {/if}
which also worked as needed.
I then proceeded seeing if I could it as a custom function (I think what
would serve this best though is custom modifier. Defined as such based on
default:
function smarty_mod_isset($string, $default="")
{
if (!isset($string) || empty($string))
return $default;
else
return $string;
}
This is then registered as
$smarty->register_modifier("isset", "smarty_mod_isset");
Called from template as:
{$description|isset}
Should also produce the same results with cleaner templates than all the
if's. But I am receiving the same error message about undefined index:
description.
Can anybody offer any potential next steps on this?
Dan
> From: Monte Ohrt <monte@ispi.net>
> Date: Fri, 11 Jan 2002 08:26:59 -0600
> To: smarty@lists.ispi.net
> Subject: [smarty] Re:Question about handling undefined variables
>
> If you do this a lot, you could clean it up with a custom function:
>
> {$description|ifset}
>
> Where "ifset" would only print the value if set, otherwise nothing.
>
> "Brian E. Lozier" wrote:
>>
>> {if isset($description)}
>> {$description}
>> {/if}
>>
>> (it was in the message just previous to yours on this list :P)
>>
>> ----- Original Message -----
>> From: Dan Thompson <djthompson@yahoo.com>
>> To: smarty@lists.ispi.net
>> Date: 1/10/02 4:06:10 PM
>>
>>>
>>> Hello,
>>>
>>> I am using Smarty for a system that I am building and am really impressed
>>> with what I have been able to do so far. But I have a question about how to
>>> handle undefined variables. What I have is a template that resembles a form
>>> that I am filling out. Because not all of the items may have answers, the
>>> PHP code before Smarty may or may not have defined/assigned all of the
>>> variables that are on the template. Therefore, when displaying the processed
>>> template, I receive error messages like "Warning: Undefined index:
>>> description in ... on line ..."
>>>
>>> My assumption would have been that Smarty would have handled this and output
>>> nothing if a variable was not assigned (maybe with an error in the popup
>>> debug console). But it doesn't, so I have tried the following
>>>
>>> 1) {if $description neq ""} {$description} {/if}
>>>
>>> 2) {$description | default: ""}
>>>
>>> 3) {assign var="description" value=$description | default: ""}
>>> {$description}
>>>
>>> None of these work. They all produce the same error.
>>>
>>> Can somebody provide some advice on how to handle this situation? My guess
>>> is something simple that I am missing. Thanks.
>>>
>>>
>>>
>>> _________________________________________________________
>>>
>>> Do You Yahoo!?
>>>
>>> Get your free @yahoo.com address at
>>> http://mail.yahoo.com
>>>
>>>
>>>
>>>
>>> ---
>>> You are currently subscribed to smarty as: brian@massassi.net
>>> To unsubscribe send a blank email to $subst('Email.Unsub')
>>> Visit Smarty's homepage at
>>> http://www.phpinsider.com/php/code/Smarty/
>>
>> ---
>> You are currently subscribed to smarty as: monte@ispi.net
>> To unsubscribe send a blank email to $subst('Email.Unsub')
>> Visit Smarty's homepage at
>> http://www.phpinsider.com/php/code/Smarty/
>
> --
> Monte Ohrt <monte@ispi.net>
> http://www.ispi.net/
>
> ---
> You are currently subscribed to smarty as: djthompson@yahoo.com
> To unsubscribe send a blank email to $subst('Email.Unsub')
> Visit Smarty's homepage at
> http://www.phpinsider.com/php/code/Smarty/
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com