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

how to use isset without generating notice ?

 
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
mmo
Smarty n00b


Joined: 11 Dec 2018
Posts: 4

PostPosted: Tue Dec 11, 2018 3:24 pm    Post subject: how to use isset without generating notice ? Reply with quote

I get notices when using isset!

Simple example:

{assign var="varname" value="name_of_non_existing_variable"}
{$varname} is {if isset(${$varname})} set {else} not set {/if}

I get: "Notice: Undefined index: name_of_non_existing_variable in ..."

In my opinion isset should not generate a notice, since it's the whole purpose of isset to check for this situation!

P.S.: Disabling notices is no option, since I am upgrading and cleaning existing code to php7, and need all messages!
Back to top
View user's profile Send private message
mmo
Smarty n00b


Joined: 11 Dec 2018
Posts: 4

PostPosted: Wed Dec 12, 2018 7:42 am    Post subject: workaround Reply with quote

It seems to use default like

{if ${$varname}|default:""}

is a usable workaround...
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Dec 13, 2018 2:08 am    Post subject: Reply with quote

Why are you using such convoluted constructs to begin with?
Everything just to not use arrays?
Back to top
View user's profile Send private message
mmo
Smarty n00b


Joined: 11 Dec 2018
Posts: 4

PostPosted: Thu Dec 13, 2018 7:01 am    Post subject: Reply with quote

Quote:
Why are you using such convoluted constructs to begin with?



Sadly it's the way I get the data. It's an old shop system, using multiple templates, and I can not change the basic data structure without having issues with the other templates...
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sun Dec 16, 2018 8:03 am    Post subject: Reply with quote

Let's try a few variants and see which works. Even though PHP understands $$varname, we need to check to determine if Smarty parses it that way.

Also, break apart the statement so that the error gets reported against a line number with greater granularity:

Code:
{$varname}
is
{if isset(${$varname})}
set
{else}
not set
{/if}

Then try:
{if isset($$varname)}

Then try:
{if $$varname}
Back to top
View user's profile Send private message
mmo
Smarty n00b


Joined: 11 Dec 2018
Posts: 4

PostPosted: Mon Dec 17, 2018 9:10 am    Post subject: Reply with quote

test1:
{assign var='varname' value='nonexisting'}
{$varname}
is
{if isset(${$varname})}
set
{else}
not set
{/if}
output:
nonexisting is
( ! ) Notice: Undefined index: nonexisting in ...


test2:
( ! ) SmartyCompilerException: Syntax error in template "file:....html" on line 3 "{if isset($$varname)}set{/if}" - Unexpected "$varname", expected one of: "","" , ")" in

test3:

( ! ) SmartyCompilerException: Syntax error in template "file:....html" on line 3 "{if $$varname}set{/if}" - Unexpected "$varname", expected one of: "}" in


I think using '|default' is the only solution not throwing a notice! Works for me...
Back to top
View user's profile Send private message
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