Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yeoman looping in template

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 %>;
      <% } %>
  }
like image 851
David Avatar asked Mar 10 '16 19:03

David


1 Answers

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.

like image 168
Simon Boudrias Avatar answered Oct 26 '22 09:10

Simon Boudrias