Yeoman looping in template
Asked Answered
G

2

6

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 %>;
      <% } %>
  }
Gaud answered 10/3, 2016 at 19:15 Comment(0)
T
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.

Thorrlow answered 11/3, 2016 at 7:40 Comment(0)
C
-2

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.

Cud answered 10/3, 2016 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.