1

如何在 Scotty 中使用 html 页面,包括html 模板?但不是通过 Blaze,因为我不喜欢用 haskell 代码描述它的结构。它认为我应该抢劫,但如何将它与 Scotty 纠缠在一起?

4

1 回答 1

0

您可以使用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 builder
于 2016-02-24T09:02:34.883 回答