胡子模板变量的问题

时间:2018-02-22 19:32:13

标签: mustache swagger-codegen

我有一个由swagger codegen工具生成的json,这是API操作的供应商扩展:

  "vendorExtensions" : {
    "x-rate-limit" : [ {
      "name" : "max",
      "value" : 20,
      "description" : "The max amount of requests allowed within the period"
    }, {
      "name" : "period",
      "value" : 60,
      "description" : "The amount of seconds that we track the amount of requests made"
    } ]
  },

当我在胡子模板中尝试访问这些变量时,namevalue会出现空白:

    {{#vendorExtensions}}
    {{#x-rate-limit}}
    $values['{{name}}'] = {{value}};
    {{/x-rate-limit}}
    {{^x-rate-limit}}
    $values['max'] = RateLimiting::MAX_LIMIT;
    $values['period'] = RateLimiting::TIMEOUT;
    {{/x-rate-limit}}
    {{/vendorExtensions}}

在未定义x-rate-limit的路径上,它会正确打印以下内容

    $values['max'] = RateLimiting::MAX_LIMIT;
    $values['period'] = RateLimiting::TIMEOUT;

在具有的路径上,它会打印以下内容

    $values[''] = ;
    $values[''] = ;

如果我在{{.}}{{#x-rate-limit}}内打印{{/x-rate-limit}},我会收到以下信息:

{"name":"max","value":20,"description":"The max amount of requests allowed within the period"}}

所以它正确地迭代了对象数组,我可以将它们打印出来,但是我无法访问这些对象中的项目。

这里有什么我想念的吗?一切似乎都对我好吗?

1 个答案:

答案 0 :(得分:0)

尝试使用此模板:

{{#vendorExtensions.x-rate-limit}}
$values['{{name}}'] = {{value}};
{{/vendorExtensions.x-rate-limit}}
{{^vendorExtensions.x-rate-limit}}
$values['max'] = RateLimiting::MAX_LIMIT;
$values['period'] = RateLimiting::TIMEOUT;
{{/vendorExtensions.x-rate-limit}}