0

当试图在我的索引页面上定义一个“链接”字段时,我遇到了一个错误,上面写着:[ERROR] Missing field $links$ in context for item index.html,即使我已经创建了一个links字段。(至少我很确定我有......)

-- site.hs
main = hakyll $ do

    match "index.html" $ do
        route idRoute
        compile $ do
            links <- loadAll "links/*"
            let indexCtx =
                    listField "links" linkCtx (return links) `mappend`
                    constField "title" "Home"                `mappend`
                    defaultContext

            getResourceBody
                >>= applyAsTemplate indexCtx
                >>= loadAndApplyTemplate "templates/default.html" indexCtx
                >>= relativizeUrls

    match "templates/*" $ compile templateBodyCompiler


linkCtx :: Context String
linkCtx =
    field "link" $ \item -> return (itemBody item)
    defaultContext

-- index.html
<h2>Links</h2>
$partial("templates/link-list.html")$

-- templates/link-list.html
<ul>
    $for(links)$
        $link$
    $endfor$
</ul>

-- links/behance.markdown
---
title: Behance
---

[Behance](https://www.behance.net/laylow)
4

1 回答 1

1

尝试您的代码时,我没有收到此类错误。相反,我从 linkCtx 收到类型错误。可以这样纠正:

linkCtx =
    field "link" (\item -> return (itemBody item)) `mappend`
    defaultContext

或者更惯用的说法,将 lambda 替换为无点形式

linkCtx =
    field "link" (return . itemBody) `mappend`
    defaultContext

此外,如果您想加载一些项目,您应该首先匹配它们,以便 hakyll 知道它们的存在。

    match "links/*" $ compile pandocCompiler

进行上述更改后,使用:重建 site.hs stack build,链接列表将在 index.html 中呈现

于 2016-07-09T18:04:49.407 回答