javascript - Yeoman looping in template - Stack Overflow

I'm trying to use yeoman to take this JSON file:{"models": {"user": {"pro

I'm trying to use yeoman to take this JSON file:

{
  "models": {
    "user": {
      "properties": [
        {
          "name": {
            "type": "string"
          },
          "surname": {
            "type": "string"
          },
          "id": "number"
        }
      ]
    }
  }
}

And turn it into something like:

Class User {
  name : string
  surname : string
  id : number
}

Would it be possible to do some form of looping in the template? Here's what I have in mind...

  export class <%= entityName %> extends Model   {
      <% forEach (property in props) { %>
         <%= property.name %> : <% property.type %>;
      <% } %>
  }

I'm trying to use yeoman to take this JSON file:

{
  "models": {
    "user": {
      "properties": [
        {
          "name": {
            "type": "string"
          },
          "surname": {
            "type": "string"
          },
          "id": "number"
        }
      ]
    }
  }
}

And turn it into something like:

Class User {
  name : string
  surname : string
  id : number
}

Would it be possible to do some form of looping in the template? Here's what I have in mind...

  export class <%= entityName %> extends Model   {
      <% forEach (property in props) { %>
         <%= property.name %> : <% property.type %>;
      <% } %>
  }
Share Improve this question edited Mar 10, 2016 at 19:24 David asked Mar 10, 2016 at 19:15 DavidDavid 16.1k26 gold badges117 silver badges160 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

The template language can run any JS code. So just use normal for loop or iterations methods on arrays (arr.forEach())

export class <%= entityName %> extends Model   {
    <% for (property of props) { %>
         <%= property.name %> : <% property.type %>;
    <% } %>
}

Yeoman is using ejs as template engine. Visit their website for more information on the supported features.

I dont think you can use that kind of looping in the template. What you could do is to have a helpermethod in your script file to generate the content from your json file into a variable, and then add that variable to your template.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744383372a4571540.html

相关推荐

  • javascript - Yeoman looping in template - Stack Overflow

    I'm trying to use yeoman to take this JSON file:{"models": {"user": {"pro

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信