Hakyll example site change

I want to change the following code so that instead of creating links to the last three posts on the site, it reproduces the body of the posts in full, as in a traditional blog. Itโ€™s a little difficult for me to understand what happens next, and what were the necessary changes.

match "index.html" $ do route idRoute compile $ do let indexCtx = field "posts" $ \_ -> postList $ fmap (take 3) . recentFirst getResourceBody >>= applyAsTemplate indexCtx >>= loadAndApplyTemplate "templates/default.html" postCtx >>= relativizeUrls 
+6
source share
2 answers

This is not entirely trivial. The first step is to introduce snapshots .

As explained in the tutorial, this ensures that you can include blog postcodes in your index without first applying the templates to HTML. So you get something like:

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

Now, to display messages on the index page, you can use all $body$ messages. To do this, you just need to update templates/post-item.html to:

 <div> <a href="$url$"><h2>$title$</h2></a> $body$ </div> 
+3
source

I know this post is a bit outdated, but since it doesnโ€™t seem to be resolved, this is how I did it.

First save the snapshot as described in @jaspervdj:

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

Then for index.html upload all snapshots of messages using loadAllSnapshots :

 match "index.html" $ do route idRoute compile $ do posts <- recentFirst =<< loadAllSnapshots "posts/*" "content" let indexCtx = listField "posts" postCtx (return posts) `mappend` defaultContext 

Since the snapshot was taken before applying the default template, the value of $body$ inside $for(posts)$ will be only the content of each message template without the default template applied.

+1
source

All Articles