【问题标题】:How to run loop in JSON Template如何在 JSON 模板中运行循环
【发布时间】:2013-11-12 15:31:10
【问题描述】:

在我的应用程序中,我使用 JSON 模板来显示内容。我在模板中使用条件语句

{{#if document_id_1}} \
{{filename_1 }}
{{document_size_1}}
{{document_type_1}}
{{/if}}\ 
{{#if document_id_2}} \
{{filename_2 }}
{{document_size_2}}
{{document_type_2}}
{{/if}}\ 
 ................ 

我需要对此运行循环(for 或 while)以避免在我的代码中提及更多 if 条件。

如何在 JSON 模板中运行循环???

【问题讨论】:

    标签: json templates loops conditional-statements


    【解决方案1】:

    假设 Mustache 并假设所有文档看起来都相同并假设此结构

    {
      documents: [
        { filename: 'document_1', ... },
      ]
    }
    
    {{#documents}}
      {{ filename }}
      //other properties below
    {{/documents}}
    

    【讨论】:

    • [ { filename: 'document_1', ... }, ] 这是我的 json 数组结构。如果我如您所示更改 json 数组结构,则 json 数据不显示...
    • 你能发布一些示例数据吗?
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 2015-02-20
    • 2019-03-13
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多