Tag list field in Hakyll templates

201 Views Asked by At

So Hakyll already has a tagsField available to add to templates, but it preformats the whole list of tags into an HTML string. I could write a custom tagsField, but I'd like to separate my concerns: have Hakyll build a list of tags for each page, then pass it to the template in a listField so that I can reference it there with a $for(tags)$ ... $tags$ ... $endfor$ block.

I'm pretty new to Hakyll, and am still a novice at Haskell in general, so I can't quite figure out the typing of my function. Any help in that direction is appreciated.

1

There are 1 best solutions below

0
acdw On

I just realized this is my own question. Oh well, here's the answer I found in case someone else needs it.

I haven't tried this, but adapting from http://mattwetmore.me/posts/hakyll-list-metadata.html:

listContextWith :: Context String -> String -> Context a
listContextWith ctx s = listField s ctx $ do
    identifier <- getUnderlying
    metadata <- getMetadata identifier
    let metas = maybe [] (map trim . splitAll ",") $ M.lookup s metadata
    return $ map (\x -> Item (fromFilePath x) x) metas

listContext :: String -> Context a
listContext = listContextWith defaultContext

-- and in main, in the match postsPattern part:
tagContext = listContext "tags" <> defaultContext

Read the whole post to try and grok it. It should work.