Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to create nested objects #29

Open
nkgokul opened this issue Jun 7, 2018 · 1 comment
Open

Ability to create nested objects #29

nkgokul opened this issue Jun 7, 2018 · 1 comment

Comments

@nkgokul
Copy link

nkgokul commented Jun 7, 2018

This is a great tool. Is there support for nested objects? For example in this case items is the final object and the nesting is present because of "children" parameter. It would be great if we could use this form of nesting in templates for generation.

const items = [
{ id: 0, text: 'Andy' },
{
id: 1, text: 'Harry',
children: [{ id: 2, text: 'David' }]
},
{ id: 3, text: 'Lisa' }
];

@zmtmaster
Copy link

zmtmaster commented Jan 8, 2019

I don't know if this code work however, have you tried using something like that?

var myPartials = {
  user: `{\
    "id": {{@index}},\
    "text": "{{firstName}}",\
    ${ Math.random() > 0.5 ? `
      children: [
         {{#repeat 3}}
         {
            "id": {{@index}}\
            "text": "{{firstName}}"\
         }
         {{/repeat}}
       ]
     ` : ''}
  }`
};

var template = '{\
    "users": [\
      {{#repeat 3}}\
        {{> user}}\
      {{/repeat}}\
    ]\
  }';

var result = dummyjson.parse(template, {partials: myPartials});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants