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