如何在哈姆雷特进行变量插值?

时间:2015-07-06 07:54:39

标签: haskell interpolation yesod shakespeare-text

假设每次刷新页面时我想显示一个随机数。这是我的代码:

...
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}|]

工作正常。

我使用默认的脚手架网站。现在,当我想在#{randomX}

中使用homepage.hamlet
<div>#{randomX}

它给了我一个错误

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")

如何在hamlet文件中应用变量插值?

抱歉英文不好。

0 个答案:

没有答案