Discussion:
[hakyll] Adding a field to items loaded with loadAll
Beerend Lauwers
2016-04-27 20:50:07 UTC
Permalink
Hi all, I'd like to do the following: load a bunch of content with loadAll
and iterate over it in a for loop in a template. This part is trivial to do:

howDoIPosts <- loadAll "content/how-do-i/*/*"
let ctx = listField "how-do-i-posts" defaultContext (return howDoIPosts)

But now, I would like to add a new key to each element loaded by loadAll, a
calculated one: the value of the item's URL without its file extension,
which should be available in $url-plain$.

I've looked around in the source code, but it's not obvious to me how I
should go about this.

Does anyone have experience with this?

Kind Regards,
Beerend Lauwers
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jasper Van der Jeugt
2016-04-28 10:49:40 UTC
Permalink
Hello Beerend,

Each element in the list is rendered with the `Context` you pass in. In
your case, this is `defaultContext`. You can add this new key by simply
extending this context.

First we define a function which does what you want:

import System.FilePath (dropExtension)

urlPlainField :: Context a
urlPlainField = field "url-plain" $ \item -> do
mbFilePath <- getRoute (itemIdentifier item)
case mbFilePath of
Nothing -> return "???"
Just filePath -> return $ toUrl $ dropExtension filePath

And then we add it to some `Context` that we define for the use in the
list:

postCtx :: Context String
postCtx =
urlPlainField `mappend`
defaultContext

Now, the following should enable you to use the new `$url-plain$`:

let ctx = listField "how-do-i-posts" postCtx (return howDoIPosts)

Hope this helps,
Peace,
Jasper
Post by Beerend Lauwers
Hi all, I'd like to do the following: load a bunch of content with loadAll
howDoIPosts <- loadAll "content/how-do-i/*/*"
let ctx = listField "how-do-i-posts" defaultContext (return howDoIPosts)
But now, I would like to add a new key to each element loaded by loadAll, a
calculated one: the value of the item's URL without its file extension,
which should be available in $url-plain$.
I've looked around in the source code, but it's not obvious to me how I
should go about this.
Does anyone have experience with this?
Kind Regards,
Beerend Lauwers
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Beerend Lauwers
2016-04-29 20:01:40 UTC
Permalink
Awesome! Thanks! I made this into a blog post, because I wanted to
understand *why* this was the
solution: http://beerendlauwers.be/posts/2016-04-29-hakyll-adding-to-loadall.html

(If I misinterpreted anything in there, feel free to correct me ;) )

Thanks again!

Beerend
Post by Jasper Van der Jeugt
Hello Beerend,
Each element in the list is rendered with the `Context` you pass in. In
your case, this is `defaultContext`. You can add this new key by simply
extending this context.
import System.FilePath (dropExtension)
urlPlainField :: Context a
urlPlainField = field "url-plain" $ \item -> do
mbFilePath <- getRoute (itemIdentifier item)
case mbFilePath of
Nothing -> return "???"
Just filePath -> return $ toUrl $ dropExtension filePath
And then we add it to some `Context` that we define for the use in the
postCtx :: Context String
postCtx =
urlPlainField `mappend`
defaultContext
let ctx = listField "how-do-i-posts" postCtx (return howDoIPosts)
Hope this helps,
Peace,
Jasper
Post by Beerend Lauwers
Hi all, I'd like to do the following: load a bunch of content with
loadAll
Post by Beerend Lauwers
and iterate over it in a for loop in a template. This part is trivial to
howDoIPosts <- loadAll "content/how-do-i/*/*"
let ctx = listField "how-do-i-posts" defaultContext (return howDoIPosts)
But now, I would like to add a new key to each element loaded by
loadAll, a
Post by Beerend Lauwers
calculated one: the value of the item's URL without its file extension,
which should be available in $url-plain$.
I've looked around in the source code, but it's not obvious to me how I
should go about this.
Does anyone have experience with this?
Kind Regards,
Beerend Lauwers
--
You received this message because you are subscribed to the Google
Groups "hakyll" group.
Post by Beerend Lauwers
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...