在没有Yesod的情况下在Haskell中使用Hamlet

时间:2011-07-15 20:48:28

标签: haskell hamlet

有人能指出我如何使用没有Yesod的哈姆雷特的例子吗? http://www.yesodweb.com/book/templates是一个很好的文档,但我无法让我的ghci会话渲染一个简单的哈姆雷特模板而不会崩溃。

2 个答案:

答案 0 :(得分:16)

这是一个显示大多数基本内容的示例,包括呈现类型化URL。

{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}

import Data.Text
import Text.Blaze.Html.Renderer.String (renderHtml)
import Text.Hamlet hiding (renderHtml)

data Url = Haskell | Yesod

renderUrl Haskell _ = pack "http://haskell.org"
renderUrl Yesod   _ = pack "http://www.yesodweb.com"

title = pack "This is in scope of the template below"

template :: HtmlUrl Url
template = [hamlet|
<html>
    <head>
        #{title}
    <body>
        <p>
            <a href=@{Haskell}>Haskell
            <a href=@{Yesod}>Yesod
|]

main = do
    let html = template renderUrl
    putStrLn $ renderHtml html

输出:

<html><head>This is in scope of the template below</head>
<body><p><a href="http://haskell.org">Haskell</a>
<a href="http://www.yesodweb.com">Yesod</a>
</p>
</body>
</html>

答案 1 :(得分:3)

好吧,我们可以使用以下方法来处理URL呈现并以最愚蠢的方式进行操作:

hamVal = [$hamlet| 
<html>
    <head>
        <title>Test page
    <body>Testing
|]

test :: ByteString
test = renderHamlet (\_ _ -> "") hamVal

哪个按预期工作。我想你想做一些稍微有用的东西,但这里的小例子很好用,所以如果不知道你遇到麻烦的地方就很难说更多。