如何将集合指定为页面中的数据,而不是子文件夹中的页面?

时间:2014-05-15 16:00:38

标签: docpad

据我所知,docpad利用集合来轻松分组和列出预定要呈现的文件(例如帖子,页面)。我有一大堆pdf文档(来自期刊的出版物),我想将其指定为一个集合,这样我就可以编写一个脚本来收集并将它们全部发布到一个页面上。据我所知,你只能在集合页面上定义一次元数据,我真的不想为每个单独的出版物创建50多个页面。

我更熟悉功能主干本身用于指定集合,但我不确定这两者(docpad和amp; backbone)如何交互。

1 个答案:

答案 0 :(得分:0)

Docpad使用集合来管理项目中的文件,而不仅仅是那些计划要呈现的文件。 "文件中的文件"目录是那些需要渲染的目录。那些"文件"目录是那些不需要渲染的目录。这些文件只是复制到" out" DIR。这意味着你可以简单地将pdf放在文件目录中。这会将您的pdf添加到docpad集合中,并可以在docpad.coffee文件中访问,如下所示:

  collections:

        # Create a collection called posts
        # That contains all the documents that will be going to the out path posts
        posts: ->
            @getCollection('documents').findAllLive({relativeOutDirPath: 'posts'},[{date:-1}])
        # Create a collection called pdfs
        # That contains all the files in the pdf directory that originate in the source
        # file directory
        pdfs: ->
            @getCollection('files').findAllLive({relativeOutDirPath: 'pdfs'})

然后创建一个eco模板页面列出pdf文件(我会将其粘贴在文档下的pdf文件夹中)。

--- 
layout: 'default'
title: 'My PDF collection' 
---
<ul>
<%pdfcollection = @getCollection('pdfs').toJSON()%>
<%for pdf in pdfcollection:%>
    <li><%-pdf.url%></li>
<%end%>

</ul>

如果您不想在您的网站上显示实际的pdf文件 - 只显示一个列表 - 那么您只需将列表键入/复制到文档中并将其格式化为markdown。像这样:

--- 
layout: 'default' 
title: 'My PDF collection'
---
* [MyPdf](http://pdf.org/) - Pdf about pdfs
* [MyPdf](http://pdf.org/) - Pdf about pdfs
* [MyPdf](http://pdf.org/) - Pdf about pdfs
* [MyPdf](http://pdf.org/) - Pdf about pdfs

第三种可能性,我还没有测试过,如果你在文本文件中有一个列表并且真的必须将其内容转换为Docpad中的一个集合,我会手动创建一个与docpad文件集合分开的集合并将其指定为docpad.coffee文件中templateData的属性。这将使该集合可用于您站点内的任何页面。在docpad.coffee的events部分中,为renderBefore事件创建一个处理程序并打开列表文本文件,循环遍历它并将每行中的值添加到集合中。

fs= require('fs') 
renderBefore: (opts,next) ->
     fs.readFile file, (err,data) ->
         arr = data.split('\r\n')
         for line in arr
             #add to collection etc...
相关问题