haskellhakyll

listField always empty


I'd like to add a field to defaultContext, which will make a list of links available to all pages. Since I don't think I can change defaultContext itself, I've created a function which adds a listField to defaultContext and replaced all references to defaultContext with it. While the program complies, my new listField is empty.

This is my latest attempt.

-- 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>

Solution

  • When you are loading an item, you need to specify a compilation target with that item – see load function.

    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.

    Adding a simple (no route is required) compiler should fix it:

    match "nav/*" $ compile pandocCompiler