【问题标题】:Filtering @getFilesAtPath results in Docpad在 Docpad 中过滤 @getFilesAtPath 结果
【发布时间】:2014-09-17 17:23:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: coffeescript docpad eco query-engine


    【解决方案1】:

    就像我在回答你其他问题时所说的那样:What methods can be called on Docpad's Query tools?

    您的查询返回的对象包含一些您看不到的其他默认元数据。正如您在这里看到的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 %>
    

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多