Smarty if isset

时间:2014-05-09 20:22:38

标签: php smarty

好的,所以我有一些PHP代码,但我最近改为聪明。我想知道如何将PHP代码转换为智能代码。

PHP代码

<?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
}
?>

到目前为止我的尝试

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

但他们的摊位失败了吗?请帮帮我!

1 个答案:

答案 0 :(得分:0)

你不需要,你不应该把所有东西都变成聪明的。

在PHP中你应该这样做:

<?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);

}
?>

在Smarty你应该做那样的事情:

{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应该仅用于显示数据,你不应该为smarty分配所有任务(例如仅为了进行简单的比较而分配comples变量/ objest)

顺便说一下,我不知道你想在你的代码中使用的id是不是因为我保留了所有代码,并引入了2个聪明的变量

相关问题