【问题标题】:Heist example not working: ‘hcCompiledSplices’ is not a record selector抢劫示例不起作用:“hcCompiledSplices”不是记录选择器
【发布时间】:2016-07-31 02:44:14
【问题描述】:

我正在尝试了解如何使用 Heist,但我发现的所有示例似乎都不起作用,而且我似乎找不到与我有相同问题的其他人。我在这里找到了一些示例代码:

https://www.schoolofhaskell.com/school/to-infinity-and-beyond/older-but-still-interesting/compiled-heist-insight-with-no-snap-in-sight

但是,当我尝试运行第一个示例时,出现以下错误:

Main.hs:20:15:
    ‘hcCompiledSplices’ is not a record selector
    In the expression:
    mempty
        {hcCompiledSplices = "foo" ## splice,
        hcTemplateLocations = [loadTemplates "."]}
    In an equation for ‘heistConfig’:
        heistConfig
        = mempty
            {hcCompiledSplices = "foo" ## splice,
            hcTemplateLocations = [loadTemplates "."]}
    In the expression:
    do { let heistConfig = ...;
        heistState <- either (error "oops") id
                        <$> (runEitherT $ initHeist heistConfig);
        builder <- maybe (error "oops") fst
                    $ renderTemplate heistState "simple";
        toByteStringIO B.putStr builder }

Main.hs:22:15:
    ‘hcTemplateLocations’ is not a record selector
    In the expression:
    mempty
        {hcCompiledSplices = "foo" ## splice,
        hcTemplateLocations = [loadTemplates "."]}
    In an equation for ‘heistConfig’:
        heistConfig
        = mempty
            {hcCompiledSplices = "foo" ## splice,
            hcTemplateLocations = [loadTemplates "."]}
    In the expression:
    do { let heistConfig = ...;
        heistState <- either (error "oops") id
                        <$> (runEitherT $ initHeist heistConfig);
        builder <- maybe (error "oops") fst
                    $ renderTemplate heistState "simple";
        toByteStringIO B.putStr builder }

我做错了什么?

【问题讨论】:

  • 你在使用heist-0.14.*吗?有些东西移动了,cf。 CHANGELOG.md.

标签: haskell haskell-snap-framework heist


【解决方案1】:

使用镜头似乎是现在做事的方式。

另外,我将模板 simple.tpl 更改为使用标签 &lt;h:foo&gt;...&lt;/h:foo&gt; 而不仅仅是 &lt;foo&gt;...&lt;/foo&gt;

{-# LANGUAGE OverloadedStrings  #-}

module Lib2 where

-- import Data.Monoid
import qualified Data.Text as T
import qualified Data.ByteString as B
import Blaze.ByteString.Builder
import Control.Monad
import Control.Monad.IO.Class
import Control.Applicative
import Control.Monad.Trans.Either
import Heist
import Heist.Compiled as C
import Control.Lens

runtime :: RuntimeSplice IO T.Text
runtime = liftIO $ do
    putStrLn "Write something:"
    T.pack <$> getLine

splice :: Splice IO
splice = return $ C.yieldRuntimeText $ runtime

main = do
    let heistConfig = emptyHeistConfig
          & hcTemplateLocations .~ [ loadTemplates "." ]
          & hcCompiledSplices .~  ( "foo" ## splice )
        reportErrors errs = error ("errors: " ++ unlines errs)
    heistState <- either reportErrors id <$> 
         (runEitherT $ initHeist heistConfig)
    builder <- maybe (error "oops") fst $
         renderTemplate heistState "simple"
    toByteStringIO B.putStr builder

【讨论】:

  • 谢谢!!你能解释一下h: 部分吗?为什么你必须这样做?我现在总是必须这样做吗?
  • 我这样做是因为错误消息是这么说的。尝试仅使用&lt;foo&gt; 标记运行,您将看到完整的错误消息。毫无疑问,前缀是可配置的。
  • 真的吗?因为我只看到oops
  • 哦,另外,命名空间是 0.14 中的新功能,可以通过设置 hcNamespace .~ "" snapframework.com/blog/2014/09/24/heist-0.14-released 来禁用
猜你喜欢
  • 1970-01-01
  • 2012-06-14
  • 2012-02-02
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2014-02-25
  • 2013-06-07
相关资源
最近更新 更多