渲染heist模板不会返回任何内容 [英] Rendering a heist template returns nothing

查看:129
本文介绍了渲染heist模板不会返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入Data.String.Conversions 
导入Data.Maybe(isJust)
导入合格的Heist
导入合格的Heist.Interpreted为I
导入合格的Heist.Compiled为HeistCom
导入Heist.Internal.Types
将合格的Text.XmlHtml导入为X
导入Data.List(sortBy)
导入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 spliceConfigTrue)
案例抢夺
Right heist' - >
已呈现< - 提升$ I.renderTemplate(hsBinding heist')$ convertString fileName
case(渲染)
Just(builder,_) - >
lift $ print $ toLazyByteString builder
Nothing - >错误抢劫错误
留下 - >错误。 convertString $ show a

我以这种方式调用函数:

  renderTemplatetemplates / compareForm$ I.bindSplicetest$ I.textSpliceabcxyz

我猜这是要配置的。我还没有彻底的想过通过上面的配置。



不幸的是,上面只是产生了heist error错误(第二行)。所以我的问题是为什么?我的下一步将是调查 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.loadTemplatestemplates /]
let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations \ __> False):: SpliceConfig IO
heist< - lift $ Heist.initHeist(HeistConfig spliceConfigTrue)
heist of
Right heist' - >
已呈现< - 提升$ I.renderTemplate(hsBinding heist')$ convertString fileName
case(渲染)
Just(builder,_) - >做
html。 convertString $ toLazyByteString builder
Nothing - >错误抢劫错误
留下 - >错误。 convertString $ show a

渲染路径不应该指定目录:

  renderTemplatecompareForm$ I.bindSplicetest$ I.textSpliceabcxyz

以上编译模板可能不起作用,可能需要修改这一行(可能是(\_ - > False)
let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations(\_-> False):: SpliceConfig IO



以上是潜在的性能影响,请参阅 https://github.com/snapframework/heist/issues/102


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

I'm calling the function this way:

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

I'm guessing it's to do with the config. I have not thoroughly thought through the above config.

Unfortunately the above is just generating an error of "heist error" (the second last line). So my question is why? My next step would be to investigate the Heist.Interpreted.renderTemplate function.

解决方案

Finally figured this out... I needed to specify a template location...

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

And the render path should not have the directory specified:

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

The above might not work for compiled templates, one would probably need to modify this line (probably the (\_ -> False): let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\_ -> False):: SpliceConfig IO

There are potential performance implications with the above, see https://github.com/snapframework/heist/issues/102

这篇关于渲染heist模板不会返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆