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

Iterate over properties of an object? Get name of properties? #9

Open
ralphholzmann opened this issue Jan 21, 2011 · 19 comments
Open

Comments

@ralphholzmann
Copy link

Hello,

Looking for a way to iterate over an object structured like so:

{
  freenode : 'irc.freenode.net',
  oftc : 'irc.oftc.net',
  ipt : 'irc.ipt.net'
}

I need to output the key names and their values. After combing the docs for quite awhile, I dont see any information on how to do this.

Thanks,

Ralph

@akdubya
Copy link
Owner

akdubya commented Jan 21, 2011

This is a job for contextual helpers.

The template:

{#iter:obj}
  {key}: {value}{~n}
{/iter}

The context:

{
  iter: function(chk, ctx, bodies) {
    var obj = ctx.current();
    for (var k in obj) {
      chk = chk.render(bodies.block, ctx.push({key: k, value: obj[k]}));
    }
    return chk;
  },
  obj: {
    freenode : 'irc.freenode.net',
    oftc : 'irc.oftc.net',
    ipt : 'irc.ipt.net'
  }
}

The output:

freenode: irc.freenode.net
oftc: irc.oftc.net
ipt: irc.ipt.net

Try this out in the dust console. DRY it up by attaching iter (or whatever you want to name it) to your base context as a global helper.

@ralphholzmann
Copy link
Author

Thanks for your quick reply! This works great for single level iteration, however the following doesn't work.

Data:

{
networks: {
freenode : {
nick: 'ralphholzmann',
real_name: 'Ralph Holzmann'
},
oftc : {
...
},
ipt : {
...
}
}
}

Template:

{#iter:networks}
{key}
{#iter:value}
{key} : {value}
{/iter}
{/iter}

The second level of key and value do not display.

Aside from the implementation details, I think that this is a feature that should be natively supported by dust, rather than having to define and use a contextual helper.

@akdubya
Copy link
Owner

akdubya commented Jan 22, 2011

To generate arbitrary levels use a recursive inline partial and make sure to assign the helper to the base context (you have to cheat a bit to get it to work in the demo console). Or just write the full object inspection code in the helper.

As for native support, I guess I'm not really seeing how "iterate over a generic object" translates into useful template code. Is there some specific use case you have in mind?

@semanticprogrammer
Copy link

I believe you should provide native support for this kind of iteration including support for nested properties.
There are many cases when it will be very useful.
For example to generate some code from json based model. In fact I'd like to have it right now :)
Thank you!

@ralphholzmann
Copy link
Author

@semanticprogrammer -- I gave up on dust awhile ago. While its speed and extensibility are to be admired, basic things like this and its interpretation of falsey values (e.g. 0 is not falsey to dust) have driven me to other libs.

@semanticprogrammer
Copy link

"@semanticprogrammer -- I gave up on dust awhile ago. While its speed and extensibility are to be admired, basic things like this and its interpretation of falsey values (e.g. 0 is not falsey to dust) have driven me to other libs."
Thank you very much for your comment. Please advice me regarding picking up the best alternative to Dust.
I'm asking for the advice because I afraid that I will follow your steps if author of Dust will not response to suggestions of his users.
By the way I have filled out a new issue with request to create google group to improve user's support.

@Almad
Copy link

Almad commented Aug 27, 2012

+1 for this

@vybs
Copy link

vybs commented Aug 27, 2012

How often do we have this case? We use it quite a lot and have not seen this as a most pressing concern. logic less templates and arbitrary JSON structure is not the norm IMO.Why does not a helper work? What is the use case for native support.

@Almad
Copy link

Almad commented Aug 27, 2012

Of course, it is only matter of convenience and development speed.

@vybs
Copy link

vybs commented Aug 27, 2012

:), how does helper not speed it up?

@zzen
Copy link

zzen commented Aug 27, 2012

@vybs - in my world, you can iterate over ordered lists or unordered maps just the same… drawing the distinction there seems arbitrary… there is no reason, if you can iterate over an array, why not iterate over an object.

So the answer for "why not helper" is: consistency

@aravinda0
Copy link

Another +1 for this.

@mandric
Copy link

mandric commented Oct 26, 2012

+1 for native support

@benjajaja
Copy link

+1 IMHO using a helper is putting logic into templating, maps should be "natively" iterable like zzen points out.

@pedronasser
Copy link

+1
holy shit this is still not native?

@neogeek77
Copy link

+1 for native support

@rashad612
Copy link

We need something like 'foreach' iteration. I rewrote the previous cheat to be:

dust.helpers.iter = function(chunk, context, bodies, params) {
        var obj = params['for'] || context.current();

        for (var k in obj) {
            chunk = chunk.render(bodies.block, context.push({key: k, value: obj[k]}));
        }
        return chunk;
    };

Template:

{@iter for=obj}
  {key}: {value}{~n}
{/iter}

And our object:

{
  freenode : 'irc.freenode.net',
  oftc : 'irc.oftc.net',
  ipt : 'irc.ipt.net'
}

However, using nested iteration caused double execution time in some case.
Any effort to push this forward ?

@vybs
Copy link

vybs commented Jun 3, 2013

can we please post this on the helpers repo?

https://github.com/linkedin/dustjs-helpers

rashad612 pushed a commit to rashad612/dustjs-helpers that referenced this issue Jun 4, 2013
@rashad612
Copy link

Done: rashad612/dustjs-helpers@9039130

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