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

The {else} tag should not accept arguments silently #1089

Open
saulery opened this issue Nov 29, 2024 · 1 comment
Open

The {else} tag should not accept arguments silently #1089

saulery opened this issue Nov 29, 2024 · 1 comment
Labels
enhancement major requires new major

Comments

@saulery
Copy link

saulery commented Nov 29, 2024

Hello,

I have a specific Warranty field that tells me the warranty period that applies beyond the legal warranty. Its value can be 0 to N, 0 for no warranty extension beyond the legal warranty. So I want to display the corresponding mention only when the specific warranty is greater than zero. In addition, I have to manage the plural on the year.

The code should have been this:

{if $specific_warranty_in_year > 1}
   <p>Specific guarantee : {$specific_warranty_in_year} years</p>
{elseif $specific_warranty_in_year eq 1}
   <p>Specific guarantee : {$specific_warranty_in_year} year</p>
{/if}

I made a mistake and typed 'else' instead of 'elseif'.

{if $specific_warranty_in_year > 1}
   <p>Specific guarantee : {$specific_warranty_in_year} years</p>
{else $specific_warranty_in_year eq 1}
   <p>Specific guarantee : {$specific_warranty_in_year} year</p>
{/if}

Which to my surprise displays "Specific guarantee : 0 year" instead of causing an error.

I guess the parser doesn't know how to distinguish between 'else' and 'elseif', and doesn't know that 'else' doesn't accept arguments.

Regards,

@devthisath
Copy link

devthisath commented Dec 25, 2024

Hi,

In Smarty, the parser doesn’t strictly validate the logic of else or elseif. It processes the syntax as written. Since else doesn’t support conditions and doesn’t throw an error when one is provided, it simply ignores the condition and executes the else block whenever the preceding if condition is false. That’s why it displays "Specific guarantee: 0 year" instead of causing an error.

Furthermore, for your expected output, you can optimize the code as follows:

{if $specific_warranty_in_year > 0}
   <p>Specific guarantee: {$specific_warranty_in_year} year{$specific_warranty_in_year > 1 ? 's' : ''}</p>
{/if}

I’ve tested the optimized code on Smarty v5.4.3, and it works as expected.

Best Regards,
Thisath

@wisskid wisskid added enhancement major requires new major labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement major requires new major
Projects
None yet
Development

No branches or pull requests

3 participants