Discussion:
[hakyll] Injecting a post into a created page
k***@gmail.com
2015-11-27 10:44:05 UTC
Permalink
Hi,

Thanks for this great project!

I'm having some trouble setting up a page with the following scenario:

1. Create index.html using the the 'create' directive.
2. index.html shows the latest post in full detail.
3. index.html shows links to N latest posts.

This seems straightforward enough, and maybe it is, but I'm having trouble
"injecting" the latest post into the index.html.

Getting the latest posts is pretty easy using "loadAll" and "recentFirst",
but I don't understand how I would inject the $date$, $title$, $body$ from
the latest post etc into the context that's used to apply the template(s).
As far as I understand, the underlying resource is not really there,
because I'm creating index.html not matching against a file, therefore I
have to use makeItem "". Deviating from that results in

The compiler yielded an Item with Identifier
notes/2012-12-07-tu-quoque.markdown, but we were expecting an Item with
Identifier index.html (you probably want to call makeItem to solve this
problem)
In short, is there a way to "inject" a post into a page that is "create"-d,
rather than "match"-ed?

Many thanks!
--
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
2015-11-27 12:25:59 UTC
Permalink
Hey,

From what I can tell the compiler for the index page currently looks
like this:

compile $ do
let indexCtx = postCtx <> defaultCtx -- Or some more stuff
lastPost <- head <$> loadAll "posts/*"
loadAndApplyTemplate "templates/index.html" indexCtx lastPost
Post by k***@gmail.com
= loadAndApplyTemplate "templates/default.html" indexCtx
That is indeed a bit of a problem -- after applying the templates, you
are still returning the `lastPost` item, which is why Hakyll is
complaining, it is expecting an index item.

You can fix this by taking the result of that computation and putting it
in the correct index item:

let indexCtx = postCtx <> defaultCtx -- Or some more stuff
lastPost <- head <$> loadAll "posts/*"
loadAndApplyTemplate "templates/index.html" indexCtx lastPost
Post by k***@gmail.com
= loadAndApplyTemplate "templates/default.html" indexCtx
= makeItem . itemBody
This yields an item which has the correct identifier.

Hope this helps,
Peace,
Jasper
Post by k***@gmail.com
Hi,
Thanks for this great project!
1. Create index.html using the the 'create' directive.
2. index.html shows the latest post in full detail.
3. index.html shows links to N latest posts.
This seems straightforward enough, and maybe it is, but I'm having trouble
"injecting" the latest post into the index.html.
Getting the latest posts is pretty easy using "loadAll" and "recentFirst",
but I don't understand how I would inject the $date$, $title$, $body$ from
the latest post etc into the context that's used to apply the template(s).
As far as I understand, the underlying resource is not really there,
because I'm creating index.html not matching against a file, therefore I
have to use makeItem "". Deviating from that results in
The compiler yielded an Item with Identifier
notes/2012-12-07-tu-quoque.markdown, but we were expecting an Item with
Identifier index.html (you probably want to call makeItem to solve this
problem)
In short, is there a way to "inject" a post into a page that is "create"-d,
rather than "match"-ed?
Many thanks!
--
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.
Karl Sutt
2015-11-27 12:37:23 UTC
Permalink
Hey Jasper, I don't know how I missed that :-S

This works brilliantly! Many thanks!
Post by Jasper Van der Jeugt
Hey,
From what I can tell the compiler for the index page currently looks
compile $ do
let indexCtx = postCtx <> defaultCtx -- Or some more stuff
lastPost <- head <$> loadAll "posts/*"
loadAndApplyTemplate "templates/index.html" indexCtx lastPost
Post by k***@gmail.com
= loadAndApplyTemplate "templates/default.html" indexCtx
That is indeed a bit of a problem -- after applying the templates, you
are still returning the `lastPost` item, which is why Hakyll is
complaining, it is expecting an index item.
You can fix this by taking the result of that computation and putting it
let indexCtx = postCtx <> defaultCtx -- Or some more stuff
lastPost <- head <$> loadAll "posts/*"
loadAndApplyTemplate "templates/index.html" indexCtx lastPost
Post by k***@gmail.com
= loadAndApplyTemplate "templates/default.html" indexCtx
= makeItem . itemBody
This yields an item which has the correct identifier.
Hope this helps,
Peace,
Jasper
Post by k***@gmail.com
Hi,
Thanks for this great project!
1. Create index.html using the the 'create' directive.
2. index.html shows the latest post in full detail.
3. index.html shows links to N latest posts.
This seems straightforward enough, and maybe it is, but I'm having
trouble
Post by k***@gmail.com
"injecting" the latest post into the index.html.
Getting the latest posts is pretty easy using "loadAll" and
"recentFirst",
Post by k***@gmail.com
but I don't understand how I would inject the $date$, $title$, $body$
from
Post by k***@gmail.com
the latest post etc into the context that's used to apply the
template(s).
Post by k***@gmail.com
As far as I understand, the underlying resource is not really there,
because I'm creating index.html not matching against a file, therefore I
have to use makeItem "". Deviating from that results in
The compiler yielded an Item with Identifier
notes/2012-12-07-tu-quoque.markdown, but we were expecting an Item
with
Post by k***@gmail.com
Identifier index.html (you probably want to call makeItem to solve
this
Post by k***@gmail.com
problem)
In short, is there a way to "inject" a post into a page that is
"create"-d,
Post by k***@gmail.com
rather than "match"-ed?
Many thanks!
--
You received this message because you are subscribed to the Google
Groups "hakyll" group.
Post by k***@gmail.com
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...