Discussion:
Defining hardcoded listField in template contexts
Pavel Kretov
2014-10-19 05:09:40 UTC
Permalink
Hello everybody.
I need a help with a silly question (I'm quite new to Haskell). I'd like to
add to generated pages a block of links to various social networks. I tried
to add another listField to my context, but I couldn't guess how. Please,
tell me how to fix marked line in the snippet below:

myLinks = [
("Github", "https://github.com/..."),
("Bitbucket", "https://bitbucket.org/..."),
("Stackoverflow", "http://stackoverflow.com/user/..."),
("MyAnimeList", "http://myanimelist.net/...")]

let myLinkCtx =
constField "url" `mappend`
constField "text"

let globalContext pages posts =
listField "posts" postCtx (posts) `mappend`
listField "pages" postCtx (pages) `mappend`
* -- listField "myLinks" myLinkCtx myLinks `mappend` -- DOESN'T
WORK --*
...

create ["index.html"] $ do
route idRoute
compile $ do
let context = globalContext
(recentFirst =<< loadAll ("pages/*" .&&. hasVersion
":source"))
(recentFirst =<< loadAll ("posts/*" .&&. hasVersion
":source"))
`mappend`
constField "isIndexPage" "True"
makeItem ""
= loadAndApplyTemplate "templates/index.html" context
= loadAndApplyTemplate "templates/default.html" context
= relativizeUrls
P. S.
Also, could you explain why I have to indent items in contexts twice?
--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Jasper Van der Jeugt
2014-10-21 11:10:46 UTC
Permalink
Hey,

If you want to use `listField` for that, you have to generate a list of
items. Those need `Identifier`s, but since they're not really used you
can do something like this:

import Data.Monoid (mconcat)
import Data.String (fromString)
import Hakyll

myLinks :: [Item (String, String)]
myLinks =
[ mk "Github" "https://github.com/..."
]
where
mk name url = Item (fromString name) (name, url)

Then extracting the URL and text for such an item becomes:

myLinkCtx :: Context (String, String)
myLinkCtx = mconcat
[ field "text" $ return . fst . itemBody
, field "url" $ return . snd . itemBody
]

And finally you add this to your context:

listField "myLinks" myLinkCtx (return myLinks)

You have to indent items in `Context`s twice because that is how
let-bindings are written in Haskell.

let foo = bar qux

is legal Haskell, and so is:

let foo = bar
qux

However, when you write

let foo = bar
qux

The parser will expect an `=` followed by a right-hand side for the
binding after `qux`.

<http://learnyouahaskell.com/> is a great resource for learning Haskell.

Hope this helps,
Peace,
Jasper
Post by Pavel Kretov
Hello everybody.
I need a help with a silly question (I'm quite new to Haskell). I'd like to
add to generated pages a block of links to various social networks. I tried
to add another listField to my context, but I couldn't guess how. Please,
myLinks = [
("Github", "https://github.com/..."),
("Bitbucket", "https://bitbucket.org/..."),
("Stackoverflow", "http://stackoverflow.com/user/..."),
("MyAnimeList", "http://myanimelist.net/...")]
let myLinkCtx =
constField "url" `mappend`
constField "text"
let globalContext pages posts =
listField "posts" postCtx (posts) `mappend`
listField "pages" postCtx (pages) `mappend`
* -- listField "myLinks" myLinkCtx myLinks `mappend` -- DOESN'T
WORK --*
...
create ["index.html"] $ do
route idRoute
compile $ do
let context = globalContext
(recentFirst =<< loadAll ("pages/*" .&&. hasVersion
":source"))
(recentFirst =<< loadAll ("posts/*" .&&. hasVersion
":source"))
`mappend`
constField "isIndexPage" "True"
makeItem ""
= loadAndApplyTemplate "templates/index.html" context
= loadAndApplyTemplate "templates/default.html" context
= relativizeUrls
P. S.
Also, could you explain why I have to indent items in contexts twice?
--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
p***@spootnik.org
2015-08-04 13:47:08 UTC
Permalink
Hi Jasper,

Thanks for the recommendations here, this ties in to the same question that
I was asking.
I ended up going with your approach and wrote:

https://gist.github.com/5d066474baa712834dbb

This fails because standard items are Item String, not Item (String,
String). Is it feasible at all to provide additional K/V params to pages ?
--
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...