How to pass an object from to a block helper back

2019-07-27 06:58发布

Before Meteor 0.8, that brought blaze, I was able to pass objects to a custom block helper content like this:

return options.fn(object)

Now that with blaze, block helpers require to return a template something like this.

return Template.someTemplate

How can I achieve the old behaviour. What I want to to is the following:

I use a blockhelper

{{#blockhelper argument="something"}}
   {{somePartOfTheObject}}
{{/blockhelper}}

Then in the the blockhelper definition, I want to do some stuff based on the argument, that will return an object.

UI.registerhelper "blockhelper", () ->
   object = getStuffFrom(this.argument)
   return Template.someTemplate(object)

As needed by blaze I created a minimal template

<template name="someTemplate">
    {{> UI.contentBlock}}
</template>

Now blaze does not allow to pass an object to this template, so it can be used within the block contents.

How can I get around this?

I know the meteor-way would be to get the object in the controller. But as I want to use this for a prototyping framework, I want to be able to create said object or objects directly from the blockhelper, so someone can pass argmuents that will get converted to objects.

1条回答
The star\"
2楼-- · 2019-07-27 07:02

The solution turns out to really simple.

If I have a helper:

UI.registerhelper "blockhelper", () ->
   object = getStuffFrom(this.argument)
   return Template.someTemplate

The variables and object of the helper are actually available in the template. So you simply do:

<template name="someTemplate">
    {{> UI.contentBlock object}}
</template>
查看更多
登录 后发表回答