How to pass helper within another helper in Spacebars?

Is there any way I can pass an helper within another helper in a spacebars tag?
I’m trying to use in a button template that creates the same button with different values (create account, forgot password, next, …) depending on the context. When I call the button template I pass the value (create account, forgot password, next, …) so this is a simple pattern.

<template name="buttonSecondary">
    <div class="buttonsecondary clicked">

{{> buttonSecondary buttonSecondaryContent='Forgot password'}}

The problem is that I’m trying to use i18n (the tap:i18n package) so instead of a string “Forgot Password”, I should use {{_ "forgotPassword"}} and this is not possible: {{> buttonSecondary buttonSecondaryContent=_ "createAccount"}}
Is there a way of doing this while keeping this pattern as simple as it is?

1 Like

you dont pass helper within helper, but you can always provide arguments to helper

or you can create component in meteorhacks:flow-components which will handle the i18n etc itself so you offload application logic.

Modify your solution like this: Either…

a) just pass the i18n key and in the buttonSecondary template use the _ helper to get the actual text or

b) use UI.contentBlock instead (and this should be the preferred solution, because your intention is to pass content, not just some attribute or a small bit of text):

<template name="buttonSecondary">
    <div class="buttonsecondary clicked">

    {{_ "forgotPassword"}}

And as for nesting helpers within helpers… I think there was some way of doing it, but I can’t recall. It’s rarely useful and I haven’t needed that kind of thing in the last bunch of projects. There seem better ways of doing that.
Like creating another helper that will return/invoke another template with the params you need and/or read params from the context and pass them on to whoever needs them etc.
Just use one of the solutions above (b in this case if you can!) or unwind your thinking and restructure it. There are many other ways of accomplishing the same thing.

Soon you will be able to nest expressions and do this in some next version of Meteor:

{{> buttonSecondary buttonSecondaryContent=(_ "createAccount")}}

Thanks Iai. That’s exactly what I need: no hassle, no fuss. I’ll be waiting for that instead.
As a second thought, I’ll be using seeekr’s solution while waiting for that new feature, so thank you as well

Just to say seeekr’s solution works perfectly :smiley: Thank you :smile: The nested solution will make code more streamlined but UI.contentBlock works perfectly.

1 Like

I know this is an old article, but I wanted to confirm that Meteor does indeed now support crazy Spacebars things like

{{> widgetListItem value=(_ "widgets.label" (helperThatTakesContextArg ..) someOtherHelper)}}

Or TAP:i18n helpers like:

{{_ "widgets.label" (helperThatTakesTwoArgs .. true) someOtherHelperWithNoArgs}}

1 Like