I have setup Hakyll to generate basic tag pages from blog posts as follows:
main = do
hakyll $ do
match "preambles/*.md" $ compile $ pandocCompiler >>= relativizeUrls
tags <- buildTags "posts/*.md" (fromCapture "tags/*.html")
tagsRules tags $ \tag pattern -> do
let title = "Posts tagged \"" ++ tag ++ "\""
route idRoute
compile $ do
posts <- recentFirst =<< loadAll pattern
preamble <- loadBody $ fromFilePath ("preambles/" ++ tag ++ ".html")
let ctx = constField "title" title
`mappend` listField "posts" postCtx (return posts)
`mappend` bodyField preamble
`mappend` defaultContext
makeItem ""
>>= loadAndApplyTemplate "templates/tag.html" ctx
>>= loadAndApplyTemplate "templates/default.html" ctx
>>= relativizeUrls
...
I would like to be able to supply an optional preamble for each page.
To do this I would expect to have a markdown file per tag in a preambles directory, and attempt to use these when building the tags pages. However, I can't figure out how to make this optional, because loadSnapshot will fail if not found.
Is this an appropriate approach, and if so, how would I handle missing data?
Also, I'm not sure that bodyField is the appropriate way to pass the data to the template.