Using #with in #each or declaring variable in Blaze

Hi!

I’m rendering a list of Documents as table rows in a table.

i.e.
<table>
{{#each Collection}}
<tr>{{someData}}</tr>
{{/each}}

` My problem is that I want a data structure returned for every document in the each. The keys of this Object are then used to render the table row.

Doing something like
<table>
{{#each Collection}}

{{#with getObject}}
    <tr>{{objectKEY}}</tr>
{{/with}}

{{/each}}
</table>

doesn’t work because then the table rows only have scope of the embedded with and not outside each.

Is there anyway I could make this work? Or better yet, how can I simply just call a helper inside my each, store that as a object, and then use said object while creating my tr data?

Ah, Blaze recommends to not use each and with because it changes the data context, which is what I was seeing. Each-in seems to be the recommended way to iterate through objects. However, it seems like I could solve my problem by just using {{#let}} instead of {{#with}}. Where I can instantiate my object using {{let}} and then access it inside the {{each}} to do stuff with my <tr>s

If anyone else has any insights or recommendations, I’d be glad to hear

1 Like

Yeah {{#let}} is really useful for caching a result object and accessing properties, I recommend using that!

Otherwise you can do your own fetching / caching / helpers, but life’s too short for endless boilerplate

Thanks coagmano…endless boilerplate :joy:…Isn’t that the reason why frameworks exist?

1 Like