Simulate returning object in helper


#1

Hey, everyone!
I need to make a dynamic list of paraphs (called options) with dynamic text in each one.
I have text.jade:

each option
  p #{this}

If I pass an array of paragraph strings this returns me each paragraph alright. I would also like to pass dynamic values to each paragraph, like:

  • par1: price 100

  • par2: price 200
    And so on.
    I have a paragraphs object, which contains text and price keys. So, ideally, if Meteor would allow to pass objects to Spacebars, I would write:

    each option
    p #{this.text}: price #{this.price}

But that’s not possible unfortunately. So how can I do this?

Update: while writing this q, I’ve been brainwaved that I should dynamically populate an array in option helper and then return it.
A working solution, but not beatiful)
Can anybody come up wit smth else?


#2

The canonical way would be to separate your paragraph block into its separate template and then populate the paragraph template from your helpers or your data context.

<template name="options">
  {{#each option}}
    {{>paragraph}}
  {{/each}}
</template>

<template name="paragraph">
  <p>{{text}}: {{price}}</p>
</template>

#3

What would be the js code?


#4

It would be too arbitrary for me to come up with placeholder code, but if you can share your current template code and its helpers, I’d be happy to suggest a fix.


#5

Actually I was interested if your solution required creating their own helpers for text and price?


#6

If text and price are part of the data context, no.

If not, yes it will require helpers.

So, what you should do is set the data context to the pararaph template.


#7

Could you link me to some tutorial on contexts and stuff? I think, I’m missing smth here… (not this one: https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/ - I’m too dense to understand it)


#8

Yep, please take a look at this: https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md#inclusion-tags

The each option part in our case actually sets the data context so text and price are readily available themselves.

But, as I said, I need to see your overall template/helper code to elaborate.