Posts are tagged! Easier to browse and discover content =)

Filed under: programming

Now this blog has tags! I figured this feature out with Hakyll earlier in the summer while building Hakyll-CSS Garden Gallery, so conveniently I had the code available for reference.

In your main function, you want to create the tags object and pass it into tagRules.

tags <- buildTags "blog/*" (fromCapture "tags/*.html")

-- Generate content for the tag pages
tagsRules tags  \tag pattern -> do   
  let title = "Themes tagged \'" ++ tag ++ "\'" 
  route idRoute 
  compile  do 
      posts <- recentFirst =<< loadAll pattern 
      let ctx = 
            constField "title" title <>
            listField "posts" postCtx (return posts) <>

makeItem "" 
    >>= loadAndApplyTemplate "templates/tag.html" ctx 
    >>= loadAndApplyTemplate "templates/default.html" ctx 
    >>= relativizeUrls

Also, you want a post with tags

postCtxWithTags :: Tags -> Context String 
postCtxWithTags tags = 
    tagsField "tags" tags <>
    field "nextPost" nextPostUrl `mappend`
    field "prevPost" previousPostUrl `mappend`

Then in your post building section, you want to pass this into your pandocCompiler

    >>= saveSnapshot "teaser"
    >>= loadAndApplyTemplate "templates/post.html"    (postCtxWithTags tags)
    >>= loadAndApplyTemplate "templates/default.html" (postCtxWithTags tags)
    >>= relativizeUrls

Enjoy the post? Let me know on twitter @katychuang.