如何在Scotty中使用html页面(,包括 html模板)?但不是通过Blaze,因为我不喜欢用haskell代码描述它的结构。它认为我应该抢劫,但如何确切地把它与斯科蒂交织在一起?
发布于 2016-02-24 09:02:34
您可以使用Heist.renderTemplate将一个模板转换为Blaze.ByteString.Builder.Builder (我认为这不是blaze html,我认为没关系),然后通过Web.Scotty.raw设置。例如:
{-# LANGUAGE OverloadedStrings #-}
import Heist
import Heist.Interpreted
import Web.Scotty (scotty, get, raw, setHeader)
import Control.Monad.Trans.Either (runEitherT)
import Control.Monad.IO.Class (liftIO)
import Blaze.ByteString.Builder (toByteString)
import qualified Data.ByteString.Lazy as DBL
import qualified Data.Text.Lazy.Encoding as DT
import Text.XmlHtml
main = scotty 3000 $
get "/" $ do
-- normally you would probably load your templates from a file,
-- but to keep the example small
(Right heist) <- liftIO $ runEitherT $ initHeist emptyHeistConfig
let heist' = addTemplate "foo" [TextNode "Hello world!"] Nothing heist
(Just (builder, mime)) <- renderTemplate heist' "foo"
setHeader "Content-Type" (DT.decodeUtf8 $ DBL.fromStrict mime)
raw $ DBL.fromStrict $ toByteString builderhttps://stackoverflow.com/questions/35592164
复制相似问题