-->

What methods can be called on Docpad's Query t

2019-09-12 12:04发布

问题:

In Docpad's documentation it lists a series of Querying helpers. Here's the link. The following list appears at the bottom. Among these helpers are @getCollection, @getFiles, @getFilesAtPath, etc. But there is no indication anywhere that I have been able to locate about what kind of objects are returned by these helpers, and what sort of methods can be called on them, how does one retrieve data, etc? There are a few examples in the Docpad documentation, for example the beginner's guide shows how you'd loop through a collection of html pages in the section "ADDING A MENU LISTING FOR OUR PAGES". From this one can glean that the objects returned here have a method toJSON() that one can call on them. But what else? The documentation also explains that these helper functions are Query-Engine objects of some sort, but in the (Query-Engine documentation)[http://learn.bevry.me/queryengine/guide] I was unable to find any description of how to manipulate these objects. So my question is: does anyone know what the structure of these objects are and how they can be manipulated? Is there a document that I'm missing somewhere?

回答1:

How I understand it using simple man words:

So in docpad documents are by default interpreted as yaml files (content+metadata). So your query returns object that is this yaml file that you parse to json.

Important to know is that this yaml file is not only "your" defined metadata + content but also some additional metadata defined here http://docpad.org/docs/meta-data. So this is a lot of additional data that you can access.

I assume you use ECO templating and you probably know that it is all about CoffeeScript. So toJSON() method is just one of javascript methods that you can use. I didn't try to check it but I just noticed that anything I want to solve with coffeescript is doable in ECO. So once you parse the object returned by your query to json, you can do anything and call any method you want on JSON object.

Whenever I query for docs of files, I add: <% console.log @document %> to my "for" loop. It returns in the console the whole json object. So you can easily see what objects are returned with your query and what data you can take out of them.

Hope that helps. Cheers