-
Notifications
You must be signed in to change notification settings - Fork 125
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
Comments
This is a job for contextual helpers. The template:
The context:
The output:
Try this out in the dust console. DRY it up by attaching |
Thanks for your quick reply! This works great for single level iteration, however the following doesn't work. Data: { Template: {#iter:networks} The second level of 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. |
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? |
I believe you should provide native support for this kind of iteration including support for nested properties. |
@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 -- 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." |
+1 for this |
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. |
Of course, it is only matter of convenience and development speed. |
:), how does helper not speed it up? |
@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 |
Another +1 for this. |
+1 for native support |
+1 IMHO using a helper is putting logic into templating, maps should be "natively" iterable like zzen points out. |
+1 |
+1 for native support |
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:
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. |
can we please post this on the helpers repo? |
Hello,
Looking for a way to iterate over an object structured like so:
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
The text was updated successfully, but these errors were encountered: