Search code examples
docpad

Show only partial blog post in Docpad, with "Read More" link


I need show only partial blog post... with a "Read more" link to full blog post.

HOME: List last 5 partial/intro posts with Read More.

This is possible in Docpad?

Thanks..


Solution

  • May by

        getCuttedContent: (content) ->            
            i = content.search('<!-- Read more -->')
            if i >= 0
                content[0..i-1]                
            else
                content
    
        hasReadMore: (content) ->
            content.search('<!-- Read more -->') >= 0
    

    more

    and

            <% posts = @getCollection('posts') %>
            <% for i in [@[email protected]]: %>
                <% document = posts.at(i).toJSON() %>
                <article class="post">
                    <h3><span class="posts_date"><%= @formatDate(document.date) %></span> <a class="post_head" href="<%= document.url %>"><%= document.title %></a></h3>
                    <div class="post-content"><%- @getCuttedContent(String(document.contentRenderedWithoutLayouts)) %></div>
                    <% if @hasReadMore(String(document.contentRenderedWithoutLayouts)): %>
                    <div class="read_more"><a href="<%= document.url %>"><strong>Читать далее &rarr;</strong></a></div>
                    <% end %>
                </article>
            <% end %>
    

    posts

    and add to post

     <!-- Read more -->