Une façon de le faire serait (explication ci-dessous):
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
|]
De cette façon, nous faisons peut-être un utilisateur de m et analysons cela dans notre fichier de modèle. Remarquez le $maybe user <- m
, cela se traduit (à peu près) par
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
Il vaut donc mieux ne pas $maybe _ <- muid