2016-03-08 36 views
1

我有以下代码,只是复制粘贴和现代化(原始示例不能用最近版本的海斯特编译)从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

看起来你正在使用renderTemplateHeist.Compiled,但定义解释接头。我相信,如果你改变这一行:

set hcInterpretedSplices defaultInterpretedSplices 

这个

set hcLoadTimeSplices defaultLoadTimeSplices 

它应该工作

+0

事实上,现在的工作。非常感谢。但我现在通过加载时拼接和编译拼接有点合作。它们之间的实际区别是什么?在我在互联网上发现的一些教程中,他们只写了关于解释拼接而不是编译的拼接,所以我只是假定拼接拼接与加载时拼接相同。显然他们不是一样的。 – Sventimir

+0

查看http://snapframework.com/docs/tutorials/compiled-splices上的教程。特别是开始的段落“为了解决这个问题,我们添加了加载时间拼接的概念”。 – mightybyte

+0

我明白了。非常感谢你。 – Sventimir