How to use Smarty if else?

21 Sep

How to use Smarty if else?

Let’s assign a discount amount from PHP to the tpl file as shown below.

$smarty->assign('discount', 0); 
{if $discount gt 0}
   You have a discount and discount amount is: {$discount}
{else}
	You don't have any discount.
{/if}

Now let’s assign some string to the tpl file as shown below.
Here we compare two strings equality using ‘eq’

$smarty->assign('category', 'Electronics'); 

Then in the tpl file, we can check the category as shown below.

{if $category eq 'Electronics'}
    As you are booking an electronics product, you will get a discount of 15%.
{else}
    For this product, we have a discount of 5%.
{/if}

For detailed explanation regarding the usage of if else in tpl,
go to the link https://www.smarty.net/docsv2/en/language.function.if.tpl

Leave a Reply

Your email address will not be published. Required fields are marked *