1

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>&#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;
4

1 に答える 1

1

renderTemplatefromを使用しているようですがHeist.Compiled、解釈されたスプライスを定義しています。この行を変更すると信じています:

set hcInterpretedSplices defaultInterpretedSplices

これに

set hcLoadTimeSplices defaultLoadTimeSplices

それは動作するはずです

于 2016-03-09T09:46:40.090 に答える