Re: [hakyll] Provide the Post Body of the Recent 3 Articles

111 views
Skip to first unread message

Jasper Van der Jeugt

unread,
Sep 19, 2012, 7:28:17 PM9/19/12
to hak...@googlegroups.com
Hey,

You're probably looking for:

copyBodyToField :: String -> Page String -> Page String

You should be able to just use body afterwards (in your postitem template).

Hope this helps,
Peace,
Jasper


On Thu, Sep 20, 2012 at 2:50 AM, Nick Charlton <nickcha...@gmail.com> wrote:
> Hi,
>
> I'm trying to present the most-recent 3 articles on index.html. I can figure
> out how to get a list of titles and links, but not the article body.
>
> Ideally, I'd also reuse the post template (templates/post.html), as it'd be
> exactly the same (rather than using the content of postitem.html, etc.)
>
> I've seen stuff like:
>
> >>> setFieldPageList (take 3 . recentFirst) "templates/postitem.html"
> "posts" "posts/*"
>
> Which has provided me with a list of the more recent three. But, I'm not
> sure you then get the body of the post, inside the template.
>
> What's the best way to go about doing that?
>
> Thanks,
>
> Nick.

Nick Charlton

unread,
Sep 20, 2012, 7:59:41 AM9/20/12
to hak...@googlegroups.com
On Thursday, 20 September 2012 00:28:38 UTC+1, jaspervdj wrote:
Hey,

You're probably looking for:

    copyBodyToField :: String -> Page String -> Page String

You should be able to just use body afterwards (in your postitem template).

Ah, hah.

Excuse my ignorance, but how would I go about fitting that into, say your `match "index.html"` block?

match "index.html" $ do
        route idRoute
        compile $ readPageCompiler
            >>> requireA "tags" (setFieldA "tags" (renderTagList'))
            >>> setFieldPageList (take 3 . recentFirst)
                    "templates/postitem.html" "posts" "posts/*"
            >>> arr applySelf
            >>> applyTemplateCompiler "templates/default.html"
            >>> relativizeUrlsCompiler

---
Nick.

Jasper Van der Jeugt

unread,
Sep 20, 2012, 7:07:05 PM9/20/12
to hak...@googlegroups.com
Hey,

It would go into the compilation process for your posts, e.g.

match "posts/*" $ do
route ...
compile $
>>> applyTemplateCompiler "templates/post.html"
-- The point at which you do this obviously influences which
-- templates are applied to it
>>> arr copyBodyToField "full"
>>> applyTemplateCompiler "templates/index.html"

And then you can use $full$ in the template you pass to `setFieldPageList`.

Hope this helps,
Peace,
Jasper

Nick Charlton

unread,
Sep 22, 2012, 10:33:55 AM9/22/12
to hak...@googlegroups.com
On Friday, 21 September 2012 00:07:27 UTC+1, jaspervdj wrote:
Hey,

It would go into the compilation process for your posts, e.g.

    match "posts/*" $ do
        route ...
        compile $
            >>> applyTemplateCompiler "templates/post.html"
            -- The point at which you do this obviously influences which
            -- templates are applied to it
            >>> arr copyBodyToField "full"
            >>> applyTemplateCompiler "templates/index.html"

And then you can use $full$ in the template you pass to `setFieldPageList`.

Yes! That's perfect. Working great.

I feel like I understand Hakyll a lot more now (examples help a lot.) I'll send out another email when it's finished to add it to the examples pool.

Thanks,

Jasper Van der Jeugt

unread,
Apr 1, 2013, 1:08:07 PM4/1/13
to hakyll
Hello Oli,

This is explained in this tutorial [1]. It is useful to read the
previous tutorials as well, if you haven't done so yet [2].

[1]: http://jaspervdj.be/hakyll/tutorials/04-compilers.html#producing-a-list-of-items
[2]: http://jaspervdj.be/hakyll/tutorials.html

Hope this helps,
Peace,
Jasper

On Mon, Apr 1, 2013 at 6:15 PM, <ozz...@gmail.com> wrote:
> As a newcomer to Haykll I was wondering if someone could explain how to do
> this using Haykll 4?
>
> Looks like this thread so far has dealt with Haykll 3.x
>
> Thanks!
>
> Oli
>
>
> On Wednesday, September 19, 2012 6:50:19 PM UTC+1, Nick Charlton wrote:
>>
>> Hi,
>>
>> I'm trying to present the most-recent 3 articles on index.html. I can
>> figure out how to get a list of titles and links, but not the article body.
>>
>> Ideally, I'd also reuse the post template (templates/post.html), as it'd
>> be exactly the same (rather than using the content of postitem.html, etc.)
>>
>> I've seen stuff like:
>>
>> >>> setFieldPageList (take 3 . recentFirst) "templates/postitem.html"
>> "posts" "posts/*"
>>
>> Which has provided me with a list of the more recent three. But, I'm not
>> sure you then get the body of the post, inside the template.
>>
>> What's the best way to go about doing that?
>>
>> Thanks,
>>
>> Nick.
>
> --
> 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+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Oliver Wilkie

unread,
Apr 1, 2013, 1:14:18 PM4/1/13
to hak...@googlegroups.com
Hi Jasper,

Thanks for the links, I had already read them!

I had no trouble following those instructions to get a list of post-items (i.e. just the title and date of each post).
I found that through following those instructions I wasn't able to get the $body$ part of a post to be shown. 

Someone on the Hakyll IRC channel was able to show me this example


And so I gleaned from that site.hs what was needed. 

I was a bit confused about what Snapshots are and why they are needed?

It seemed I had to run each post through the pandocCompiler and then save them as snapshots under the name "content".

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

Then in creating my index.html I had to set the posts field to be the result of this

postListC sf = do
    posts   <- sf =<< loadAllSnapshots "posts/*" "content"
    itemTpl <- loadBody "templates/post.html"
    list    <- applyTemplateList itemTpl postCtx posts
    return list

Is this the best practice?

Cheers,
Oli

You received this message because you are subscribed to a topic in the Google Groups "hakyll" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hakyll/ouUexL1zbu0/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to hakyll+un...@googlegroups.com.

Jasper Van der Jeugt

unread,
Apr 1, 2013, 2:56:45 PM4/1/13
to hakyll
Yep, that's best practice.

The problem is that if we would just use the $body$ of the posts, this
would already include all templates (e.g. start with an <html> tag).
By using a snapshot, this problem is prevented. A similar problem
arises (and is solved) in this tutorial [1].

[1]: http://jaspervdj.be/hakyll/tutorials/05-snapshots-feeds.html#snapshots

Hope this helps,
Peace,
Jasper

Reply all
Reply to author
Forward
0 new messages