How can I specify a collection as data within a page, rather than pages within a subfolder?

StackOverflow https://stackoverflow.com/questions/23683310

  •  23-07-2023
  •  | 
  •  

문제

As I understand it, docpad utilizes collections in order to easily group and list files that are slated to be rendered (ex. posts, pages). I have a large list of pdf documents (publications from a journal) that I want to specify as a collection so I can write a script that will collect and post them all onto a single page. As far as I can tell, you can only define metadata once on a collection page, and I don't really want to create 50+ pages for each individual publication.

I'm more familiar with the functionality backbone itself uses to specify collections, but I'm not sure how the two (docpad & backbone) interact.

도움이 되었습니까?

해결책

Docpad uses collections to manage the files within a project, not just those that are slated to be rendered. Files in the "document" directory are those that need to be rendered. Those in the "files" directory are those that do not need to be rendered. Those files are simply copied to the "out" dir. What this means is that you can simply place your pdfs in the files dir. This will add your pdfs to the docpad collection and can be accessed in the docpad.coffee file like this:

  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'})

Then create an eco template page to list the pdf files (I would stick this in the pdf folder under documents).

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

</ul>

If you don't want to have the actual pdf files in your website - just display a list - then you could simply type/copy the list into a document and format it as markdown. Something like this:

--- 
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

Third possibility, which I haven't tested, if you have a list in a text file and really must convert its contents to a collection within Docpad, I would manually create a collection that is seperate from the docpad file collection and assign it as a property of templateData within the docpad.coffee file. This will make the collection available to any page within your site. In the events section of docpad.coffee create a handler for the renderBefore event and open your list text file, loop through it and add the values in each line to your collection.

fs= require('fs') 
renderBefore: (opts,next) ->
     fs.readFile file, (err,data) ->
         arr = data.split('\r\n')
         for line in arr
             #add to collection etc...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top