freemarkerjbakejbake-templates

JBake Templates: highlight latest post


I am new to JBake. I saw the default way of creating an index page.

<#list posts as post>
                <#if (post.status == "published")>
                                -- design your posts here
                </#if>
            </#list>

This pulls up all the posts in descending order.

This looks great, with only one problem which is, I am not sure how to highlight my latest post.

So I want to do something like,

<#list posts as post>
                <#if (post.status == "published")>
                               <#if (this is latest post)>
                                           use highlighted style
                                       </#if>
                                        <#if (this is not a latest post)>
                                           use normal style
                                       </#if>
                </#if>
            </#list>

How can I achieve this?


Solution

  • Here is one solution that works for JBake v2.4.0:

            <#list posts as post>
                <#if (post.status == "published")>
                    <#if (post_index == 0)>
                        //apply highlight style
                    <#else>
                        //apply normal style
                    </#if>
                </#if>
            </#list> 
    

    To speed up the rendering of the page you could just use the published_posts variable too:

            <#list published_posts as post>
                <#if (post.status == "published")>
                    <#if (post_index == 0)>
                        //apply highlight style
                    <#else>
                        //apply normal style
                    </#if>
                </#if>
            </#list> 
    

    If you update JBake to use Freemarker v2.3.23 instead you can then use post?is_first instead of post_index == 0.