Search code examples
haskellhakyll

Multi posts per pages


I'm making a website with Hakyll and I would like to gather some posts in the same page. I find somes example on websites such as dannysu or EAnalytica, but my code is not working and I don't understand why.

My code compile fine, the trace function show that all the posts are loaded, the compilation is successfull but all the webpages supposed to contain 3 posts are empty. Only the text before the loop and the links to the next and previous pages are visible.

enter image description here

My Hakyll code is the following.

toolsPages = do
  pag <- buildPaginateWith grouper "tools/*" makeId
  paginateRules pag $ \pageNum pattern -> trace (show pag) $ do
      route idRoute
      compile $ do
          posts <- recentFirst =<< loadAll pattern
          let paginateCtx = paginateContext pag pageNum
              ctx =
                  constField "title" ("Tools - Page " ++ (show pageNum)) <>
                  listField "tools" (postCtx) (return posts) <>
                  paginateCtx <>
                  defaultContext
          makeItem ""
              >>= loadAndApplyTemplate "templates/outils-pag.html" ctx
              >>= relativizeUrls

grouper ids = (liftM (paginateEvery 3) . sortRecentFirst) ids

makeId pageNum = fromFilePath $ "blog/page/" ++ (show pageNum) ++ "/index.html"

postCtx :: Context String
postCtx =
  dateField "date" "%d / %m / %Y"
  `mappend` bodyField "body"
  `mappend` defaultContext

The templates/outils-pag.html template contains the following.

some text before the loop
$for(tool)$
some text inside the loop
$date$
$teaser$
$body$
$endfor$

$if(previousPageNum)$
<a href="$previousPageUrl$">previous</a>
$else$
$endif$
&mdash;
$currentPageNum$ of $numPages$
&mdash;
$if(nextPageNum)$
<a href="$nextPageUrl$">next</a>
$else$
$endif$

Do you have an idea of what is wrong with my code?
Do you know another example of pagination with Hakyll?


Solution

  • This basic compile rule must be run before pagination :

    tools = do
      match "tools/*" $ do
        route $ setExtension "html"
        compile $ compiler
          >>= loadAndApplyTemplate "templates/tools.html"    postCtx
          >>= relativizeUrls
    

    And then, it Work!