The {else} tag should not accept arguments silently
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,
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