smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.25k stars 705 forks source link

4.3.0 - Ternary exact match (=== and !===) giving incorrect results #861

Closed coopan closed 1 year ago

coopan commented 1 year ago

Smarty version: 4.3.0 PHP version: 7.4

We recently upgraded from 3.1.47 to 4.3.0. We have a few templates that use ternary conditions with exact matches that were giving some weird output after the upgrade.

We had a bit of a play around with some literals in templates and reproduced the issue, confirming that switching back to 3.1.47 corrected the issue. Also tested exact match operator in a normal if condition. It seems this is specific to exact match and exact not match only when used in a ternary. In reality we are comparing a literal to a variable, so the below isolated type issues coming from those variables.

Examples

{if (1 === 1)}
    1 equals 1<br />
{else}
    1 ain't 1<br />
{/if}
{(2 === 2) ? '2 equals 2' : '2 ain\'t 2'}<br />
{(3 == 3) ? '3 equals 3' : '3 ain\'t 3'}<br />
{(4 !== 4) ? '4 ain\'t 4' : '4 equals 4'}

On 4.3.0

1 equals 1 2 ain't 2 3 equals 3 4 ain't 4

On 3.1.47, all seems as expected

1 equals 1 2 equals 2 3 equals 3 4 equals 4

I've had a dig through the issues here and the changelog, but don't see anything about a change to ternary operator. Have we missed something in the 4.x upgrade?

wisskid commented 1 year ago

I didn't even know we support the ternary... we do have a feature request for it. Will have to look into this

wisskid commented 1 year ago

OK, we definitely support the ternary. There are no unit tests for it though and it's not documented either. Anyway, I cannot reproduce this on any PHP-version (7.2 t/m 8.2)

v4.3.0

1 equals 1<br />
2 equals 2<br />
3 equals 3<br />
4 equals 4
scottchiefbaker commented 1 year ago

I'm running Smarty v4.2.1 on PHP v8.0.27 and I cannot confirm either. Copy and pasting the exact example above I get:

1 equals 1
2 equals 2
3 equals 3
4 equals 4 
coopan commented 1 year ago

Thanks @wisskid and @scottchiefbaker!

Very weird and points to something odd with our setup. Strange that it would exhibit this behaviour on 4.3.0 but simply using composer to revert to 3.1.47 and nothing else corrects it.

We'll have a dig and post back here when we know. Do you know what version ternary was added? I wonder if predecessors have added an extension somewhere to support it on 3.x and it doesn't play nice with 4.x.

scottchiefbaker commented 1 year ago

I thought it might have something to do with your PHP but I tested the sample above with PHP: v7.0, v7.1, v7.2, v7.3, v7.4, v8.0, and v8.1 and they all work fine.