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

modifier default not working with @ suppression #336

Closed
alabama opened this issue Feb 22, 2017 · 7 comments
Closed

modifier default not working with @ suppression #336

alabama opened this issue Feb 22, 2017 · 7 comments
Milestone

Comments

@alabama
Copy link

alabama commented Feb 22, 2017

The default modifier just access the tpl_vars array with the non existing key and tries to suppress the error outcome with '@' like:
$tmp = @$_smarty_tpl->tpl_vars['bShowInfo']->value

This won't work in PHP >= 5.4 (5.3 as well?)
There should be at least an "array_key_exist | isset" check to get the functionality right

@Miksser
Copy link

Miksser commented Oct 17, 2018

Is it possible to get rid of suppression in 'default'?
This behavior is very disturbing during debugging through xDebug =(

1 similar comment
@Miksser
Copy link

Miksser commented Oct 17, 2018

Is it possible to get rid of suppression in 'default'?
This behavior is very disturbing during debugging through xDebug =(

@antman3351
Copy link

Had the same issue when debuging, I had a lots of notices in the console. Plus a possible performance loss from the notice being issued ( https://stackoverflow.com/questions/1868874/does-php-run-faster-without-warnings )

Maybe it's worth changing the original function?
Until then here's my workaround

/**
 * Overwrites smarty's default modifier
 * Original function used $tmp = @$_smarty_tpl->tpl_vars['param0']->value)===null||$tmp==='' ? $_smarty_tpl->tpl_vars[param1'']->value : $tmp)
 * Witch throws a notice that is silenced
 * @param array $params
 * @return mixed|string
 */
function smartyDefault( $params )
{
    $default = $params[1] ?? '';
    return "( isset({$params[0]}) && {$params[0]} !== '' ) ? {$params[0]} : {$default}";
}

 $smarty->registerPlugin('modifiercompiler', 'default', 'smartyDefault' );

@wisskid
Copy link
Contributor

wisskid commented Mar 22, 2021

@Miksser @antman3351 I've added an empty check for variables for |default modifier in #650 . It does not appear to break anything. Can you confirm this fixes your problem?

@antman3351
Copy link

I just had a quick look at the change, so I might have missed something, but doesn't this change the behaviour of the default modifier?
before default was used for unset variable, null or an empty string. Using empty() default would also be applied to false, 0 , '0' and [ ].

@wisskid
Copy link
Contributor

wisskid commented Mar 22, 2021

Well spotted, @antman3351 ! Not sure how that got past me. I thought I ran the new unit tests against the old code (seeing no change in behavior) but I must have somehow messed that up.
Added the 0, '0' and [] cases to the unit tests and fixed the implementation.
Is this better?

@wisskid wisskid added this to the next milestone Sep 12, 2022
@wisskid
Copy link
Contributor

wisskid commented Sep 22, 2022

The default modifier has already been changed to longer use the @ modifier in #629

@wisskid wisskid closed this as completed Sep 22, 2022
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

Successfully merging a pull request may close this issue.

4 participants