Coffeescript 在Docpad中筛选@getFilesAtPath结果

Coffeescript 在Docpad中筛选@getFilesAtPath结果,coffeescript,docpad,eco,query-engine,Coffeescript,Docpad,Eco,Query Engine,在Docpad中,以下代码(使用查询引擎帮助程序和)从目录树中提取文件名列表,并将其url添加到数组中: <% images = []; %> <% for file in @getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %> <% images.push(file.url) %> <% end %> 如何将查询限制为文件的子集,例如仅PNG?如我在回答您的其他问

在Docpad中,以下代码(使用查询引擎帮助程序和)从目录树中提取文件名列表,并将其url添加到数组中:

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


如何将查询限制为文件的子集,例如仅PNG?

如我在回答您的其他问题时所述:

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

extension:'png'
因此,您的代码可能如下所示(注意findAll部分,它提供了设置搜索条件的可能性):


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

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

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