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> <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>
事实上,现在的工作。非常感谢。但我现在通过加载时拼接和编译拼接有点合作。它们之间的实际区别是什么?在我在互联网上发现的一些教程中,他们只写了关于解释拼接而不是编译的拼接,所以我只是假定拼接拼接与加载时拼接相同。显然他们不是一样的。 – Sventimir
查看http://snapframework.com/docs/tutorials/compiled-splices上的教程。特别是开始的段落“为了解决这个问题,我们添加了加载时间拼接的概念”。 – mightybyte
我明白了。非常感谢你。 – Sventimir