0

I'm struggling with what was supposed to be simple and I can't get it. Is there a bug or is it just me? I've created two simple examples using two approaches: 1)registerHelper and 2)parentData: The HTML is:

<body>
    This is the body
   {{> A}}
</body>

 <template name="A">
     This is "A"
        {{> B}}
    </template>

    <template name="B">
        This is "B"
        {{> C}}
    </template>

    <template name="C">
        This is "C"
        {{foo}}
    </template>

And the Javascript for 1) is:

 Template.registerHelper(foo, function () {
    return 'Hello';
});

And for case 2) is:

Template.A.helpers({
  foo: function () {
      return 'Hello';
  }
});

Template.C.helpers({
  foo: function () {
    return Template.parentData(2);
  }
});

What is wrong?

Paulo Janeiro
  • 3,181
  • 4
  • 28
  • 46

1 Answers1

0

It looks like you are trying to invoke the helper function infoMessage of the parent template. This is not the same as accessing the parent data context. For your use-case, I recommend registering a helper function that any template, including A and B, can use:

Template.registerHelper("infoMessage", function (dataContext) {
    return dataContext.infoMessage;
});

EDIT: With your edited question, you are missing the quotation marks around "foo" in (1):

Template.registerHelper("foo", function () {
  return 'Hello';
});

Then use it in your templates:

<template name="A">
    <div class="message info-message">{{> B}}{{infoMessage .}}</div>
</template>

<template name="B">
    <div class="wrap-popup">
        <div class="popup-img"><img src="check12.svg"></div>
        <div class="popup-item">
            <h1> Sucess</h1>
            <p>{{infoMessage ..}}Nada...</p>
        </div>
    </div>
</template>

See official Meteor docs on registering helpers for more.

FullStack
  • 5,902
  • 4
  • 43
  • 77
  • Sorry for the delay but I've been on holiday. Yes this could be a good approach indeed. But my problem is that it seems that NOTHING is working: 1) I've created a simple example using registerHelper and it doesn't work; 2) I've created a simple example using parentData and it's also not working. Is it my version or there is a bug? I've changed my example above. – Paulo Janeiro Jun 01 '15 at 09:31
  • Looks like you are missing the quotation marks around "foo" in your revised question (1). Try adding that and let us know if it works. – FullStack Jun 01 '15 at 12:36
  • You're 100% right on that...(what a rookie mistake...). Do you have a clue about the Template.parentData? I also tried to use on the HTML {{../../foo}} instead of the javascript helper and it still doesn't work...(I also had a typo that I corrected above). – Paulo Janeiro Jun 01 '15 at 14:56
  • You should just go one level up for parent data, i.e. {{../parentField}}. Your comment above is confusing because you can't do that if "foo" is a template helper function. I'm happy to help if you explain more what you're trying to do. – FullStack Jun 02 '15 at 05:31
  • I'm trying to customize the style of the accounts-ui-unstyled package and I want to replace hardcoded button names with buttons that I create inside a template and then put instead of the original name. I would like to use existing helpers that set buttons names and labels dynamically using existing helpers. I'm including my button templates inside existing templates where those helpers pass them correctly the names I need so I though it could be a good use case for using parentData. But I'm not able to make a simple proof of concept using it..can you make a simple example that works using it? – Paulo Janeiro Jun 02 '15 at 07:08
  • How do you decide what names to assign buttons? – FullStack Jun 03 '15 at 10:19