繁体   English   中英

渲染抢劫模板不会返回任何内容

[英]Rendering a heist template returns nothing

import Data.String.Conversions
import Data.Maybe (isJust)
import qualified Heist
import qualified Heist.Interpreted as I
import qualified Heist.Compiled as HeistCom
import Heist.Internal.Types
import qualified Text.XmlHtml as X
import Data.List (sortBy)
import Data.Map.Syntax
import Data.ByteString.Builder (toLazyByteString)

renderTemplate :: String -> (HeistState IO -> HeistState IO) -> ActionM ()
renderTemplate fileName hsBinding = do
  let emptyI = return () :: MapSyntax Text (I.Splice IO)
  let emptyC = return () :: MapSyntax Text (HeistCom.Splice IO)
  let emptyA = return () :: MapSyntax Text (AttrSplice IO)
  let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA [] (\_ -> False):: SpliceConfig IO
  heist <- lift $ Heist.initHeist (HeistConfig spliceConfig "" True)
  case heist of
    Right heist' -> do
      rendered <- lift $ I.renderTemplate (hsBinding heist') $ convertString fileName
      case (rendered) of
        Just (builder, _) -> do
          lift $ print $ toLazyByteString builder
        Nothing -> error "heist error"
    Left a -> error . convertString $ show a

我这样调用该函数:

renderTemplate "templates/compareForm"  $ I.bindSplice "test" $ I.textSplice "abcxyz"

我猜这与配置有关。 我没有仔细考虑以上配置。

不幸的是,以上只是产生了“抢劫错误”的错误(第二行)。 所以我的问题是为什么? 我的下一步是研究Heist.Interpreted.renderTemplate函数。

终于弄明白了...我需要指定一个模板位置...

renderTemplate :: String -> (HeistState IO -> HeistState IO) -> ActionM ()
renderTemplate fileName hsBinding = do
  let emptyI = return () :: MapSyntax Text (I.Splice IO)
  let emptyC = return () :: MapSyntax Text (HeistCom.Splice IO)
  let emptyA = return () :: MapSyntax Text (AttrSplice IO)
  let templateLocations = [Heist.loadTemplates "templates/"]
  let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\_ -> False):: SpliceConfig IO
  heist <- lift $ Heist.initHeist (HeistConfig spliceConfig "" True)
  case heist of
    Right heist' -> do
      rendered <- lift $ I.renderTemplate (hsBinding heist') $ convertString fileName
      case (rendered) of
        Just (builder, _) -> do
          html . convertString $ toLazyByteString builder
        Nothing -> error "heist error"
    Left a -> error . convertString $ show a

并且渲染路径不应具有指定的目录:

renderTemplate "compareForm"  $ I.bindSplice "test" $ I.textSplice "abcxyz"

上面的内容可能不适用于已编译的模板,可能需​​要修改这一行(可能是(\\_ -> False) let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\\_ -> False):: SpliceConfig IO (\\_ -> False)let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\\_ -> False):: SpliceConfig IO

以上内容可能会对性能产生影响,请参阅https://github.com/snapframework/heist/issues/102

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM