0

在 Docpad 中,以下代码(使用 Query-Engine 助手和eco)从目录树中提取文件名列表并将其 url 添加到数组中:

<% images = []; %>
<% for file in @getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %>
    <% images.push(file.url) %>
<% end %>

我如何将查询限制为文件的子集,只说 PNG?

4

1 回答 1

1

就像我在回答您的另一个问题时所说的那样:Docpad 的查询工具可以调用哪些方法?

您的查询返回的对象具有一些您看不到的其他默认元数据。正如您在这里看到的http://docpad.org/docs/meta-data,元数据之一是“扩展”。因此,您可以使用以下条件进行查询:

extension:'png'

所以你的代码可能看起来像(注意 findAll 部分,它可以让你设置搜索条件):

<% images = []; %>
<% for file in @getFilesAtPath({relativeOutDirPath: 'images/'}).findAll(extension:'png').toJSON() : %>
    <% images.push(file.url) %>
<% end %>

或者,如果您想返回所有文件并在不同的扩展上触发不同的操作,您可以:

<% images = []; %>
<% for file in @getFilesAtPath(relativeOutDirPath: 'images/').toJSON() : %>
    <% if file.extension is 'png' : %>
        <% images.push(file.url) %>
    <% end %>
<% end %>
于 2014-09-21T17:13:24.217 回答