4

这是我的代码:

 testSplice :: C.Splice Handler
 testSplice = return $ C.yieldRuntimeText $ do
    return "中文"

我将它绑定到一个标签:

  splices :: Splices (C.Splice Handler)
  splices =
      "testSplice" ## testSplice

并在 layout.tpl 上使用它:

   <meta charset="UTF-8"> 
   <testSplice/>

浏览器上的输出是

    -�

我做错了什么?


抱歉耽搁了,我忙了一段时间,现在我回来了,我想我可能没有把问题说得足够具体@mightybyte这是出现问题的代码,我希望它会使问题更加说明:

测试.hs:

{-# LANGUAGE OverloadedStrings #-}
import Snap
import Heist
import qualified Heist.Compiled as C
import Data.Monoid
import Control.Monad.Trans.Either
import Data.Maybe

main :: IO ()
main = quickHttpServe site

site :: Snap ()
site = 
    route [("/", testSnap)]

testSnap :: Snap ()
testSnap = do
    hs <- liftIO $ load "template" splices
    let runtime = fromJust $ C.renderTemplate hs "test"
    builder <-liftIO  (fst runtime)
    writeBuilder builder
  where
    splices :: Splices (C.Splice IO)
    splices = 
        "testSplice" ## testSplice

load :: MonadIO n
    => FilePath
    -> Splices (C.Splice n)
    -> IO (HeistState n)
load baseDir splices = do
    tmap <- runEitherT $ do
        let t = loadTemplates baseDir
            hc = HeistConfig
                    defaultInterpretedSplices
                    defaultLoadTimeSplices
                    splices
                    mempty
                    [t]
        initHeist hc
    either (error . concat) return tmap

testSplice :: C.Splice IO
testSplice = return $ C.yieldRuntimeText $ do return "中文" 

模板/test.tpl

<html>
  <head>
    <meta charset="UTF-8">
  </head>
 <body>
     <testSplice/>
 </body>
 </html>

现在我尝试了 heist-0.13.0.2,它现在工作正常,Daniel 干得好!

4

1 回答 1

3

更新:此答案中描述的问题已在heist版本0.13.0.2中得到纠正。

源代码yieldRuntimeText

yieldRuntimeText :: Monad n => RuntimeSplice n Text -> DList (Chunk n)
yieldRuntimeText = yieldRuntime .  liftM fromText

该功能来自哪个模块fromText?在导入部分我们发现:

import           Blaze.ByteString.Builder
import           Blaze.ByteString.Builder.Char8

后一个包的文档说:

注意:此软件包旨在用于实现协议等低级用途。如果您需要序列化 ​​Unicode 字符,请使用其中一种 UTF 编码(例如,'Blaze.ByteString.Builder.Char.UTF-8')。

并且:

fromText :: Text -> BuilderSource

O(n). Serialize the lower 8-bits of all characters in the strict text.

嗯,也许问题是从 Text 到 Builder 的编码不是在 UTF-8 中完成的?尝试使用完全相同的代码定义您自己的 yieldRuntimeText 版本,但改用以下导入:

import           Blaze.ByteString.Builder
import           Blaze.ByteString.Builder.Char.Utf8

yieldRuntimeTextUtf8 :: Monad n => RuntimeSplice n Text -> DList (Chunk n)
yieldRuntimeTextUtf8 = yieldRuntime .  liftM fromText
于 2013-10-12T07:22:39.753 回答