haskellheist

Heist not substituting templates


I have the following code, just copy-pasted and modernised (the original example does not compile with recent versions of Heist anymore) from 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

And the following template:

<!-- 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>

Running this program outputs to the console the whole template (almost) as is. No substistutions are made. Probably there's some function call missing, required by modern Hesit versions. I was trying to track it down in the documentation, but no luck. Why doesn't it work?

Output:

<!-- 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;

Solution

  • It looks like you are using renderTemplate from Heist.Compiled, but defining interpreted splices. I believe if you change this line:

    set hcInterpretedSplices defaultInterpretedSplices
    

    to this

    set hcLoadTimeSplices defaultLoadTimeSplices
    

    it should work