List of posts in a post page

77 views
Skip to first unread message

Giedrius Kudelis

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

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" <>
    defaultContext

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

postCtx :: Context String
postCtx =
    postListCtx <>
    plainPostCtx

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">
    $for(allPosts)$
        <li>
            <a href="$url$">$title$</a> - $date$
        </li>
    $endfor$
</ul>

If you need to have a look at the whole repo: https://github.com/gkudelis/gkudelis.github.io/tree/source

I've had a look at the list of pages using hakyll and this does what I want: http://dannysu.com/2015/10/31/regex-nfa/ (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

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

I had a look at https://groups.google.com/forum/#!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?

Regards,
Giedrius Kudelis

Robin Powell

unread,
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; http://rlpowell.name/computing/general/hakyll_recent_posts.html for details.
Reply all
Reply to author
Forward
0 new messages