一种方法是(以下说明):
getHomeR :: Handler Html
getHomeR = do
muid <- maybeAuthId
m <- case muid of
Nothing -> return Nothing
Just i -> runDB $ get i
defaultLayout $ do
[whamlet|
<p>Welcome!
$maybe user <- m
<p>
Welcome #{userIdent user}<br>
<a href=@{AuthR LogoutR}>Logout
$nothing
<p>
<a href=@{AuthR LoginR}>Go to the login page
|]
这样,我们将可能的用户从M中删除,然后在我们的模板文件中解析。注意 $maybe user <- m
, ,这个(粗略)转化为
case m of
Just user -> do
-- Do stuff with user, which is now of type User (and not Maybe User)
Nothing -> do
--Do stuff if m was nothing
因此,最好不要通配符 $maybe _ <- muid