谁能给我举个例子,说明如何在没有Yesod的情况下使用Hamlet?http://www.yesodweb.com/book/templates是一个很好的文档,但是我的ghci会话无法在不崩溃的情况下呈现一个简单的hamlet模板。
发布于 2011-07-16 05:31:20
这里有一个示例,展示了大多数基本内容,包括类型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>发布于 2011-07-16 05:11:20
好吧,用最愚蠢的方式渲染URL并做一些事情,我们可以使用这个:
hamVal = [$hamlet|
<html>
<head>
<title>Test page
<body>Testing
|]
test :: ByteString
test = renderHamlet (\_ _ -> "") hamVal它的工作方式与预期一致。我想你想要做一些稍微有用的事情,但是这里的小例子工作得很好,所以很难在不知道哪里有问题的情况下说更多的话。
https://stackoverflow.com/questions/6712974
复制相似问题