How to do variable interpolation in hamlet?

258 Views Asked by At

Say i want to display a random number everytime i refresh a page. Here's my code:

...
import System.Random
...

getHomeR :: Handler Html
getHomeR = do
  randomX <- liftIO $ randomRIO (1,999::Int)
  defaultLayout $ do
    setTitle "Welcome To Yesod!"
    $(widgetFile "homepage")
    toWidgetBody [hamlet|<div>#{randomX}|]

That works fine.

I use the default scaffolded site. Now when I want to use #{randomX} in homepage.hamlet

<div>#{randomX}

It gives me an error

Handler/Home.hs:37:11:
Not in scope: ‘randomX’
Perhaps you meant one of these:
  ‘randomR’ (imported from System.Random),
  ‘random’ (imported from System.Random),
  ‘randoms’ (imported from System.Random)
In the splice: $(widgetFile "homepage")

How do i apply variable interpolation in a hamlet file?

Sorry for bad english.

0

There are 0 best solutions below