[Solved]In Spacebars README - Nested Helper

In Spacebars README,

Nested Helper: If there is a positional argument followed by other
(positional or keyword arguments), the first argument is called on the others
using the normal helper argument calling convention.

Is the first argument a positional argument ? and
why the first argument is called on the other positional or keyword arguments ?

I attempted to understand this but I can’t.
Can anyone please describe more about this ? or give examples ?

1 Like

Hi,

This means exactly what it is. A nested helper. So if you have for example:

{{#blockHelper nestedHelper arg1}}

or

{{> inclusionHelper nestedHelper arg1='arg1'}}

nestedHelper will execute first, taking arg1 for positional (or keyword) argument. Then *Helper will take the value returned from nestedHelper and execute.

I’m not sure I explained it very well so if you still don’t understand it I will try again :smile:

EDIT: This is working only for inclusion and block helpers.

2 Likes

@thinklinux Hello. I have some questions.

Are you sure that arg1 is delivered to nestedHelper ?

After I tested as your example above, arg1 is only delivered to helper as second parameter(never delivered to nestedHelper), and the return value of nestedHelper(which is declared as helper function) is delivered to helper as first parameter.

Are you testing with inclusion tags or block tags ? I don’t think this is working for other than that but I have to test it.

No, I just tested with Double-braced template tag as you explained above.

hello.js
Template.hello.helpers({
    helper: function (arg1, arg2) {
          console.dir(arg1);   // “returned value from nestedHelper”
          console.dir(arg2);   // “arg1”
     },
     nestedHelper: function (arg1, arg2) {
         console.dir(arg1);   // undefined
         console.dir(arg2);   // undefined
         return “returned value from nestedHelper”;
     }
});

hello.html
{{helper nestedHelper “arg1”}}

Can you please share the result after you test it ?

@barkbaek I tested it and you are right. It works only for inclusion and block helpers. I will update my answer above.

Thank you for reply @thinklinux.

However, I’m still curious of how you wrote the code for blockHelper and inclusionHelper, because I only understood that Nested Helper is working for the way you explained when it is used with inclusion tags and block tags.

Can you please share more detailed examples for blockHelper and inclusionHelper as a code?

Also I’m curious of what happens when there are more than one NestedHelper.

@barkbaek I will give you an example with block helper - {{#each}}` over objects

You can find a lot like this if you do a search on google. The nested helper gives you more control of what data context you set on inclusion or block helper.

Also I’m curious of what happens when there are more than one NestedHelper

You can always test that and see the result :wink:

Thank you @thinklinux ! I solved my curiosity by your favor :slight_smile:

1 Like