'Smarty if isset

Okay so I have some PHP code but I have recently changed to smarty. And I was wondering how I convert that PHP code to smarty code.

PHP CODE

<?php 
if (isset($_SESSION['user']['id']))
{
     echo "You are logged in using method 1";
}
if (Users::isuser())
{
     echo "You are logged in using method 2
}
?>

My Attempt so far

{if isset($_SESSION['user']['id'])} You are logged in using method 1 {/if}
{if Users::isuser } You are logged in using method 2 {/if}

But they booth fail? Help me please!



Solution 1:[1]

You don't need to and you shouldn't convert everything to smarty.

In PHP you should do something like this:

<?php 
$smarty->assign('logged_method',0); 
$smarty->assign('logged_method_2',0); 

if (isset($_SESSION['user']['id']))
{
     $smarty->assign('logged_method',1);  
}
if (Users::isuser())
{
     $smarty->assign('logged_method_2',1);

}
?>

In Smarty you should do something like that:

{if $logged_method eq 1} You are logged in using method 1 {/if}
{if $logged_method_2 eq 1} You are logged in using method 2 {/if}

Smarty should be used only for displaying data and you should not to assign all tasks to smarty (for example, assigning complex variables/objects only to make simple comparisons)

By the way I don't know if you wanted to use in your code else or not so I left all the code unchanged and introduced 2 smarty variables

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raphael75