Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isset() check fails within section $VAR[loop].key syntax if "key" or "loop" does not exist #453

Closed
chrisknerr opened this issue Jun 14, 2018 · 2 comments

Comments

@chrisknerr
Copy link

chrisknerr commented Jun 14, 2018

Hello there!

I recently updated the Smarty library in a project from v3.1.30 to v3.1.32 and noticed, that this version seems to have a problem with the isset() check if a var is not set and I'm using the syntax you usually use to address something in a section with $VAR[counter].subvalue. As soon I roll back to v3.1.30 the same template code works again so I think it must have to do something with a change made in 3.1.31 or 3.1.32. I'm using both with PHP7.2 and error_reporting is set to strict including all notices for uninitialized vars in my dev environment.

Has the syntax just changed or does [] fail now due to a bug? (It worked on 3.1.30).

I made an example for you here:

- PAGE START
{section name=topics loop=$TOPICS}
  {if isset($TOPICS[topics].thisoneexists)}
     this one exists
  {/if}

  {if isset($TOPICS[topics].thisdoesnotexist)}
     this one does not exist
  {/if}

  {if isset($TOPICS[bla].blubb)}
     this doesn't even nearly exist ;)
  {/if}
{/section}
- PAGE END

With Smarty v3.1.30 I see:

- PAGE START
this one exists
- PAGE END

so this is the expected behaviour: The vars that are not existing in the section array fail the isset check, but the template gets compiled further to the end.

However on Smarty v3.1.32 I see:

- PAGE START
this one exists

In other words: As soon I have an isset with a non-existing array key the template stops to compile further and I end up just seeing a blank page. No message in the error log, no message when I turn on Smarty Debugging ... very mysterious.

The funny thing is when I write it like this {if isset($TOPICS.topics.thisdoesnotexist)} it seems to work.

EDIT: Correction, this is also no option to solve the problem! I just noticed the method with dots doesn't work in sections. This will always return a false isset() check even if a value is set. So it seems I definitely need the $TOPICS[topics].thisdoesnotexist Syntax here.

Am I doing something wrong here or is that a bug? It makes me wonder that the syntax with [] does NOT fail if the value is really set and only fails when its not set, so if you got rid of the [] variant and there is a different syntax for sections, shouldn't this fail with a syntax error in both cases then? Or shouldn't it behave like in 3.1.30 where using that worked correctly and nonexistant values didn't cause the template compiling to stop/fail?

Thanks in advance!

@chrisknerr chrisknerr changed the title isset() check fails within section [] syntax if key does not exist? isset() check fails within section [] syntax if key does not exist Jun 14, 2018
@chrisknerr chrisknerr changed the title isset() check fails within section [] syntax if key does not exist isset() check fails within section $VAR[loop].key syntax if key does not exist Jun 14, 2018
@chrisknerr chrisknerr changed the title isset() check fails within section $VAR[loop].key syntax if key does not exist isset() check fails within section $VAR[loop].key syntax if "key" or "loop" does not exist Jun 14, 2018
@pszalko
Copy link

pszalko commented Mar 12, 2019

@chrisknerr You may take a look at workaround introduced in #499 (comment) I hope this solves your issue too!

@chrisknerr
Copy link
Author

chrisknerr commented Mar 12, 2019

Thank you very much!

Your workaround does the trick in some cases like the ones you mentioned in your referenced comment and is a good solution for the moment I think! Unfortunately this seems not to work with all use cases (like checking or using a var from a loop directly like in my first example). So I still hope for an official fix to that especially because the old behaviour saves you from creating additional temporary vars with already existing content. I'm currently using some older compiler files for Smarty to work in production and just manually implemented the critical fixes that came from 3.1.30 to 3.1.33 and did some project specific additional blocks to the smarty files.

For me this was currently the best option because I didn't need to change a lot of template syntax for that. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants