他说不要替换模板

时间:2016-03-08 09:35:58

标签: haskell heist

我有以下代码,只是复制粘贴和现代化(原始示例不再使用最新版本的Heist编译)here

{-# LANGUAGE OverloadedStrings #-}
module Main where

import qualified Data.ByteString.Char8 as BS
import Data.Monoid
import Data.Maybe
import Data.List
import Control.Applicative
import Control.Lens
import Control.Monad.Trans
import Control.Monad.Trans.Either
import Heist
import Heist.Compiled
import Blaze.ByteString.Builder


conf :: HeistConfig IO
conf =  set hcTemplateLocations [ loadTemplates "." ] $
        set hcInterpretedSplices defaultInterpretedSplices $
        emptyHeistConfig


runHeistConf :: Either [String] (HeistState IO) -> IO (HeistState IO)
runHeistConf (Right hs) = return hs
runHeistConf (Left msgs) = error . intercalate "\n" $ map ("[Heist error]: " ++) msgs


main :: IO ()
main = do
    heist <- id <$> (runEitherT $ initHeist conf) >>= runHeistConf
    output <- fst $ fromMaybe (error "xxx") $ renderTemplate heist "billy"

    BS.putStrLn . toByteString $ output

以下模板:

<!-- billy.tpl -->
<bind tag="wanted">Playstation 4</bind>
<bind tag="got">Monopoly board game</bind>

<apply template="letter">
  <bind tag="kiddo">Billy</bind>
  I regret to inform you the "<wanted />" you have requested is currently
  unavailable. I have substituted this with "<got />". I hope this does not
  disappoint you.
</apply>

运行此程序将整个模板(几乎)按原样输出到控制台。没有任何替代品。现代Hesit版本可能需要一些函数调用缺失。我试图在文档中跟踪它,但没有运气。为什么它不起作用?

输出:

<!-- billy.tpl --><bind tag='wanted'>Playstation 4</bind>&#10;<bind tag='got'>Monopoly board game</bind>&#10;
<apply template='letter'>
  <bind tag='kiddo'>Billy</bind>
  I regret to inform you the "<wanted></wanted>" you have requested is currently
  unavailable. I have substituted this with "<got></got>". I hope this does not
  disappoint you.
</apply>&#10;

1 个答案:

答案 0 :(得分:1)

看起来您正在使用renderTemplate中的Heist.Compiled,但定义了解释的拼接。我相信如果你改变这一行:

set hcInterpretedSplices defaultInterpretedSplices

到这个

set hcLoadTimeSplices defaultLoadTimeSplices

它应该有用