我一直在测试docpad作为CMS,我想知道如何在我的主页上显示来自博客的最新5篇文章。
我已经找过例子了,但到目前为止还没找到。
我需要一些插件来实现这个功能吗?目前,我正在使用以下模块:
"docpad-plugin-marked": "~2.1.1",
"docpad-plugin-stylus": "~2.3.0",
"docpad-plugin-coffeekup": "~2.1.5",
"docpad-plugin-cleanurls": "~2.4.3",
"docpad-plugin-coffeescript": "~2.2.1",
"docpad": "~6.32.0",
"docpad-plugin-minicms": "~2.1.1"发布于 2013-05-16 21:01:28
在docpad.coffee文件中的集合下,我有
posts: ->
@getCollection('documents').findAllLive({relativeOutDirPath:path.join('blog','post')},[date:-1])我想这里的关键是按照date属性对集合进行排序("date:-1")
然后在你的"eco“文件中,你可以使用:@getCollection(‘post’)来访问这个集合。当然,这将为您提供所有的帖子-因此,如果您只想要最后n个帖子,那么您只需要集合中的前n个文档。
<% for document in @getCollection('posts').toJSON().slice(0,5): %>
<li>
<a href="<%=document.url%>" title="<%=document.title%>"><%=document.title%>
</a>
</li>
<% end %>发布于 2013-05-15 08:58:22
您不需要插件,可以使用内置的@getFilesAtPath帮助器
例如,我将我的博客文章存储在src/documents/blog中,并在我的主页上使用
<% for post in @getFilesAtPath("blog").findAll().toJSON() %>
<a href="<%= post.url %>">
<%= post.title %>
</a>
<% end %>我还没有正确阅读docpad中集合的文档,所以我使用了Array#slice的一个快速技巧,将我的博客帖子限制在3上
<% for post in @getFilesAtPath("blog").findAll().toJSON().slice(0, 3) %>https://stackoverflow.com/questions/16515158
复制相似问题