0

When trying to define a "links" field on my index page, I'm met with an error that says: [ERROR] Missing field $links$ in context for item index.html, even though I have created a links field. (At least I'm pretty sure I have ...)

-- 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)
pdoherty926
  • 9,895
  • 4
  • 37
  • 68

1 Answers1

1

When trying your code, I get no such error. Instead I get a type error from linkCtx. It can be corrected like this:

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

Or more idiomatically, replacing the lambda with point-free form.

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

Also if you want to load some items, you should match them first so hakyll knows about their existence.

    match "links/*" $ compile pandocCompiler

After making the changes listed above, rebuild site.hs using: stack build and the list of links will be rendered in index.html

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
  • Thanks for the reply. Unfortunately, introducing your code doesn't change anything - which leads me to believe I'm doing something else incorrectly. Do you know if it's possible that I'm running into [this](http://blog.gmane.org/gmane.comp.lang.haskell.hakyll/month=20130601) issue? – pdoherty926 Jul 09 '16 at 18:26
  • 1
    Can you share an output of rebuilding with `-v` argument (e.g. `stack exec -- site rebuild -v`) – Jan Tojnar Jul 09 '16 at 19:15
  • [Here's](https://gist.github.com/ethagnawl/e849ed70639c8b87630418efc8fbdc40) the output of that command and [the latest code](https://github.com/ethagnawl/peterdohertys.website). Thanks, again. – pdoherty926 Jul 09 '16 at 19:45
  • That’s weird, the site content rebuilds just fine for me. Did you rebuild the haskell code of the site itself (`stack build`)? – Jan Tojnar Jul 09 '16 at 19:56
  • Arg - that was it. I was working under the assumption that that was handled by `stack exec site build`. However, now that I'm re-reading the tutorial, I see that it's stated very clearly that it is not. Thanks for your help. – pdoherty926 Jul 09 '16 at 20:02