为什么我无法在 Hakyll 中定义任意字段?
Why am I unable to define an arbitrary field in Hakyll?
当我试图在我的索引页上定义一个 "links" 字段时,我遇到了一个错误:[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)
尝试您的代码时,我没有收到此类错误。相反,我从 linkCtx 收到类型错误。可以这样更正:
linkCtx =
field "link" (\item -> return (itemBody item)) `mappend`
defaultContext
或者更通俗地说,将 lambda 替换为 point-free form。
linkCtx =
field "link" (return . itemBody) `mappend`
defaultContext
此外,如果你想加载一些项目,你应该先匹配它们,以便 hakyll 知道它们的存在。
match "links/*" $ compile pandocCompiler
进行上面列出的更改后,使用 stack build
重建 site.hs,链接列表将在 index.html
中呈现
当我试图在我的索引页上定义一个 "links" 字段时,我遇到了一个错误:[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)
尝试您的代码时,我没有收到此类错误。相反,我从 linkCtx 收到类型错误。可以这样更正:
linkCtx =
field "link" (\item -> return (itemBody item)) `mappend`
defaultContext
或者更通俗地说,将 lambda 替换为 point-free form。
linkCtx =
field "link" (return . itemBody) `mappend`
defaultContext
此外,如果你想加载一些项目,你应该先匹配它们,以便 hakyll 知道它们的存在。
match "links/*" $ compile pandocCompiler
进行上面列出的更改后,使用 stack build
重建 site.hs,链接列表将在 index.html