|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mmo Smarty n00b
Joined: 11 Dec 2018 Posts: 4
|
Posted: Tue Dec 11, 2018 3:24 pm Post subject: how to use isset without generating notice ? |
|
|
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 |
|
mmo Smarty n00b
Joined: 11 Dec 2018 Posts: 4
|
Posted: Wed Dec 12, 2018 7:42 am Post subject: workaround |
|
|
It seems to use default like
{if ${$varname}|default:""}
is a usable workaround... |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Dec 13, 2018 2:08 am Post subject: |
|
|
Why are you using such convoluted constructs to begin with?
Everything just to not use arrays? |
|
Back to top |
|
mmo Smarty n00b
Joined: 11 Dec 2018 Posts: 4
|
Posted: Thu Dec 13, 2018 7:01 am Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun Dec 16, 2018 8:03 am Post subject: |
|
|
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 |
|
mmo Smarty n00b
Joined: 11 Dec 2018 Posts: 4
|
Posted: Mon Dec 17, 2018 9:10 am Post subject: |
|
|
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 |
|
|
|
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
|