1

Given the JSON object:

errors =
{ 
  hashed_password: { 
     message: 'Validator "Password cannot be blank" failed for path hashed_password',
     name: 'ValidatorError',
     path: 'hashed_password',
     type: 'Password cannot be blank' },
  username: { 
     message: 'Validator "Username cannot be blank" failed for path username',
     name: 'ValidatorError',
     path: 'username',
     type: 'Username cannot be blank' },
  email: {
     message: 'Validator "Email cannot be blank" failed for path email',
     name: 'ValidatorError',
     path: 'email',
     type: 'Email cannot be blank' },
  name: { 
     message: 'Validator "Name cannot be blank" failed for path name',
     name: 'ValidatorError',
     path: 'name',
     type: 'Name cannot be blank' } 
}

How do I iterate through the properties of each "current context" object?

I would think you do something like this:

{#errors}
    {#.}
         {type}
    {/.}
{/errors}
1
  • Root issue: in what order would you want your output to be in? Commented Mar 2, 2015 at 16:47

3 Answers 3

4

If you really have to put meaningful data into object keys, consider writing a contextual helper, as per:

Sign up to request clarification or add additional context in comments.

Comments

0

It is not possible to iterate through members of an object in Dust. Part of the reason is that you cannot know the order of the members. Another part is that this is seen as bringing too much logic into Dust.

Instead, you can change the JSON to look more like this:

{
  errors: [
    {
      hashed_password: { 
        message: 'Validator "Password cannot be blank" failed for path hashed_password',
        name: 'ValidatorError',
        path: 'hashed_password',
        type: 'Password cannot be blank'
      }
    },
    {
      username: { 
        message: 'Validator "Username cannot be blank" failed for path username',
        name: 'ValidatorError',
        path: 'username',
        type: 'Username cannot be blank'
      }
    },
    {
      email: {
        message: 'Validator "Email cannot be blank" failed for path email',
        name: 'ValidatorError',
        path: 'email',
        type: 'Email cannot be blank'
      }
    },
    {
      name: { 
        message: 'Validator "Name cannot be blank" failed for path name',
        name: 'ValidatorError',
        path: 'name',
        type: 'Name cannot be blank'
    }
  ]
}

1 Comment

So could you site a snippet of the most efficient way of doing what I'm trying to do?
0

You can iterate over an object using a helper.

For example, you could define a helper like this one:

dust.helpers.iter = function(chunk, context, bodies, params) {
  var obj = dust.helpers.tap(params.obj, chunk, context);

  var iterable = [];

  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      var value = obj[key];

      iterable.push({
        '$key': key,
        '$value': value,
        '$type': typeof value
      });
    }
  }

  return chunk.section(iterable, context, bodies);
};

Then, in your template, you would loop through like this:

{@iter obj=errors}
  {$value.type}
{/iter}

1 Comment

Although that technically works, keep in mind that the order the elements will be rendered in is not defined, and might even change every time you render the template. You might want to build the list of keys into an array, sort it and only then iterate on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.