List of posts in a post page

Skip to first unread message

Giedrius Kudelis

Dec 6, 2015, 11:59:34 AM12/6/15
to hakyll

I'm trying to get all my post pages to show a list of all posts on the blog and link to them. Kind of like an archive on every page in the sidebar. I got the list of posts to be shown using versions. The post titles and dates are correct, however the link URLs are all pointing to the current page. Here's what I have in my site.hs:

main :: IO ()
main = hakyll $ do

    (stuff here...)

    match "posts/*" $ do
        route $ setExtension "html"
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/post.html" postCtx
            >>= loadAndApplyTemplate "templates/base.html" postCtx
            >>= relativizeUrls

    -- get unchanged versions too for rendering the post list
    match "posts/*" $ version "plain" $ do
        compile $ getResourceBody

    (more stuff here...)

plainPostCtx :: Context String
plainPostCtx =
    dateField "date" "%B %e, %Y" <>

postListCtx :: Context String
postListCtx =
    listField "allPosts" plainPostCtx (recentFirst =<< (loadAll $ "posts/*" .&&. hasVersion "plain"))

postCtx :: Context String
postCtx =
    postListCtx <>

The base template (templates/base.html) uses a partial (templates/post-list.html) which uses the allPosts list. Here's what I have in post-list.html:
<ul class="post-list">
            <a href="$url$">$title$</a> - $date$

If you need to have a look at the whole repo:

I've had a look at the list of pages using hakyll and this does what I want: (it has a list of most recent posts in the footer). I had a look at the code, but could not figure out what I'm doing wrong. Please help, it feels like I'm missing some small detail, but I'm new to haskell and hakyll and I'm feeling pretty clumsy :).

Best Regards,
Giedrius Kudelis

Giedrius Kudelis

Dec 6, 2015, 1:26:42 PM12/6/15
to hakyll
Hi again,

I had a look at!topic/hakyll/F2j7iztwKEc and at the tutorial again and figured out what was missing. After I add the line

    route $ setExtension "html"

to the "plain" version - everything works! Sorry to bother :).

The other approach in that thread was using getMetadata, but I imagine I won't be able to get the urls that way. Correct?

Giedrius Kudelis

Robin Powell

Dec 30, 2020, 12:12:40 AM12/30/20
to hakyll
Your post helped me find a way to handle a related problem I was having; for details.
Reply all
Reply to author
0 new messages