2013-06-18 56 views

回答

4

月由

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

    hasReadMore: (content) -> 
     content.search('<!-- Read more -->') >= 0 

more

 <% posts = @getCollection('posts') %> 
     <% for i in [@[email protected]ndIdx]: %> 
      <% 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

的D收藏張貼

<!-- Read more --> 
0

如果您更前後更希望在不同的頁面,你可以與他們Splitting a Document into Multiple Pages example使用paged plugin

喜歡的東西:

--- 
title: 'Awesome Pages Post' 
layout: 'default' 
isPaged: true 
pageCount: 2 
pageSize: 1 
--- 

<!-- Page Content --> 
before more 
if @document.page.number is 1: %> 
    after more 
<% end %> 

<!-- Page Listing --> 
<% if @document.page.number is 0: %> 
    <!-- Read More Button --> 
    <a href="<%= @getNextPage() %>">Read more!</a></li> 
<% end %> 

應該做的伎倆。然後你可以定義邏輯來處理不同的用例。例如,這兩個頁面上都會顯示「before more」文本。但是如果你願意的話,你可以在「第0頁」檢查中包裹「before more」。

0

如果您不想爲之前和之後的其他頁面創建不同的頁面,但只希望在內容列表之前使用更多頁面。你可以把你以前更多的東西在「說明」元數據屬性,像這樣:

--- cson 
title: 'Awesome Pages Post" 
layout: "default" 
description: """ 
    Before more content goes here 
    """ 
--- 

After more content (the actual page content) goes here. 

然後,你可以通過執行顯示內容列表說明:

<%- post.description or post.contentRenderedWithoutLayouts %> 

這將回退到如果描述未定義,則爲完整內容。

如果您希望能夠描述描述,請參閱text plugin。您的元數據描述更改,而不是以下:

description: """ 
    <t render="markdown">With the text plugin **you can render anything providing you have the plugins installed!**</t> 
    """ 
0

正如另一種方式,我用下面的方法docpad.coffee截斷顯示職位主頁上。它處理的鏈接,這將使文字看起來更長,並可能最終打破在中間的報價

# Used for shortening a post 
truncateText: (content,trimTo) -> 
    trimTo = trimTo || 200 
    output = content.substr(0,trimTo).trim() 
    #remove anchor tags as they don't show up on the page 
    nolinks = output.replace(/<a(\s[^>]*)?>.*?<\/a>/ig,"") 
    #check if there is a difference in length - if so add this 
    #difference to the trimTo length - add the text length that will not show 
    #up in the rendered HTML 
    diff = output.length - nolinks.length 
    output = content.substr(0,trimTo + diff) 
    #find the last space so that we don't break the text 
    #in the middle of a word 
    i = output.lastIndexOf(' ',output.length-1) 
    output = output.substr(0,i)+"..." 
    count1 = (output.match(/<blockquote>/g) || []).length 
    count2 = (output.match(/<\/blockquote>/g) || []).length 
    if count1 > count2 
     output += "</blockquote>" 
    return output 
相關問題