listField 始终为空

listField always empty

我想向 defaultContext 添加一个字段,这将使所有页面都可以使用链接列表。由于我认为我无法更改 defaultContext 本身,因此我创建了一个函数,将 listField 添加到 defaultContext 并用它替换了对 defaultContext 的所有引用。当程序符合要求时,我的新 listField 是空的。

这是我最近的尝试。

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

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

match "templates/*" $ compile templateBodyCompiler

myCtx =
  listField "navItems" defaultContext (loadAll "nav/*") `mappend`
  defaultContext

-- nav/item.markdown
---
title: nav item 1
---

-- templates/default.html
<ul>
    $for(navItems)$
        $title$
    $endfor$
</ul>

当您加载一个项目时,您需要为该项目指定一个编译目标——参见load函数。

load :: (Binary a, Typeable a) => Identifier -> Compiler (Item a)

Load an item compiled elsewhere. If the required item is not yet compiled, the build system will take care of that automatically.

添加一个简单的(不需要 route)编译器应该修复它:

match "nav/*" $ compile pandocCompiler