I'm trying to generate HTML for posts in Hakyll that have a versions
entry in their metadata. For example, a post may have versions: Python 3.4, pytest 1.5.2
which would be formatted nicely at the bottom of the post.
To achieve this, I want to create a context which loads the metadata and creates a ListField
. Something like the following stub:
versionsCtx :: Context String
versionsCtx = listFieldWith "versions" ctx (\item -> do
versions <- getMetadataField (itemIdentifier item) "versions"
return $ case versions of
Just lst -> map (mkVersinoItem . trim) $ splitAll "," lst
Nothing -> [])
where ctx = field "version" (return . itemBody)
mkVersionItem version = Item {
itemIdentifier = fromString ("version/" ++ version),
itemBody = version
}
In my post.html
template, I have:
...
<section>
$body$
$if(versions)$
<hr />
<ul>
$for(versions)$
<li>$version$</li>
$endfor$
</ul>
$else$
<p>Fail...</p>
$endif$
</section>
...
Yet I have tried many different definitions of versionsCtx
and found similar attempts online. None seem to work and the post is always rendered with "Fail...". What am I doing wrong?
EDIT: Updated question with suggestions and clarifications.