-
Notifications
You must be signed in to change notification settings - Fork 714
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
v4 -> v5: Undefined Variables are Treated Differently #1075
Comments
In v5 you will not get any warnings unless you set
|
Also I think it's a matter of preference in a template language if you want these kind of warnings. Annotating everything with ?? is a pain |
Correct. I mentioned this. But it has the side effect of also triggering warnings for I know PHP !== Smarty, but having the behavior of empty and isset mimic that of PHP is my preference. |
@j-applese3d sorry didn't read your question fully. I agree that using isset modifier, empty or ?? should not trigger a warning on error_unassigned |
I tried upgrading a site to version 5, but ran into some issues that I could not resolve.
Specifically, the way Smarty handles variables has changed.
Example:
Output:
Smarty v4.5.4
With a stack trace potentially and more similar warnings.
Smarty v5.4.1
There is no indication that the variable was never assigned.
Why's this a problem?
I would like to know when I make a typo, or forget to assign a variable, but with v5 it'll just become the empty string.
Just use
error_unassigned
?Yes, setting
error_unassigned
when using v5 will trigger the error, but the problem with this fix is that it will ALSO trigger an error if an undefined variable is used in combination with empty or isset. Like:{if empty($undefinedVar)} Sweet! {else} Oh no! {/if}
PHP's
empty
andisset
work in a special way, suppressing warnings about undefined stuff, and simply returning a boolean.So, I don't see an easy way to fix this without adding significant complexity to the compiler, so that it treats empty and isset in a special/different way when attempting to access variables/values.
The difference between v4 and v5 is significant in how they work, as v4 compiles to direct array access, whereas v5 uses $_smarty_tpl->getVariable().
The relevant compiled code of the above example is:
Am I doing something wrong?
Following an anti-pattern?
Is it unreasonable to expect
{if empty($neverAssignedVar)}
to work, while also expecting an error if{$neverAssigned}
is used?I apologize If I missed something in the docs!
The text was updated successfully, but these errors were encountered: