Reputation: 15
I try to put a session value in a variable to display it in my .hamlet but it does not focntion!
getEtatR :: Handler Html
getEtatR = do
mSessionValue <- lookupSession "myKey"
let myValue = mSessionValue :: Maybe Text
defaultLayout $ do
aDomId <- newIdent
setTitle "mon titre"
$(widgetFile "etatWidget")
I need #{myValue} to put it in my etat.hamlet
Upvotes: 1
Views: 151
Reputation: 12070
The problem is the type of myValue, which is Maybe Text. In order for a variable to show up in the template, it has to be an instance of Text.Blaze.ToMarkup.... So Text, String, or Int would all work, but "Maybe a" does not.
There are many ways to convert a "Maybe Text" to a ToMarkup. If you know for sure that the Maybe will not be a "Nothing", just strip the maybe using fromJust (imported from Data.Maybe).... But beware that if it ever does come up as a Nothing the program will crash. Similarly you could use a case statement to fill in the Nothing case, like this
myVariable = case mSessionValue of
Just x -> x
Nothing -> "<No session value>"
You can also do a quick check by converting mSessionValue to a string using show.
The following works for me....
getEtatR :: Handler Html
getEtatR = do
mSessionValue <- lookupSession "myKey"
let myValue = show mSessionValue
defaultLayout $ do
aDomId <- newIdent
setTitle "mon titre"
$(widgetFile "etatWidget")
using etatWidget.hamlet
<h1>#{myValue}
Upvotes: 1
Reputation: 2076
If all you want is to display the value and get it out of Maybe, you can do this directly inside the hamlet
$maybe val <- mSessionValue
<p>#{val}
$nothing
<p>No Value Set
Upvotes: 0