最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Yeoman looping in template - Stack Overflow

programmeradmin9浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论