抢劫不替换模板
Heist not substituting templates
我有以下代码,只是从 here.
复制粘贴和现代化(原始示例不再使用最新版本的 Heist 编译)
{-# 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> <bind tag='got'>Monopoly board game</bind>
<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>
您似乎在使用 Heist.Compiled
中的 renderTemplate
,但定义的是解释拼接。我相信如果你改变这一行:
set hcInterpretedSplices defaultInterpretedSplices
至此
set hcLoadTimeSplices defaultLoadTimeSplices
它应该有效
我有以下代码,只是从 here.
复制粘贴和现代化(原始示例不再使用最新版本的 Heist 编译){-# 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> <bind tag='got'>Monopoly board game</bind>
<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>
您似乎在使用 Heist.Compiled
中的 renderTemplate
,但定义的是解释拼接。我相信如果你改变这一行:
set hcInterpretedSplices defaultInterpretedSplices
至此
set hcLoadTimeSplices defaultLoadTimeSplices
它应该有效