【问题标题】:Heist not substituting templates抢劫不替代模板
【发布时间】:2016-03-09 17:46:40
【问题描述】:

我有以下代码,只是从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;

【问题讨论】:

    标签: haskell heist


    【解决方案1】:

    看起来您正在使用来自Heist.CompiledrenderTemplate,但定义了解释拼接。我相信如果你改变这一行:

    set hcInterpretedSplices defaultInterpretedSplices
    

    到这里

    set hcLoadTimeSplices defaultLoadTimeSplices
    

    它应该可以工作

    【讨论】:

    • 确实,现在可以了。非常感谢。但是我现在对加载时拼接和编译拼接有点困惑。它们之间的实际区别是什么?在我在互联网上找到的少数教程中,他们只写了解释拼接而不是编译拼接,所以我只是假设编译拼接与加载时拼接相同。但显然它们并不相同。
    • 查看教程snapframework.com/docs/tutorials/compiled-splices。特别是开头的段落“为了解决这个问题,我们添加了加载时间拼接的概念”。
    • 我明白了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多