The fastest way for transferring data between few nested templates


#1

I have three nested each other templates:

TemplateOne
  TemplateTwo
    TemplateThree

But only TemplateOne has own data context.

I’m interested in the fastest way to get first template’s data context in the third template.

  1. Using “templating system” in html file: {{> TemplateThree data=…/… }}
  2. Using “helper system” TemplateThree.helpers({ data: func() {return Template.parentData(2)}

How do you think, what way is the fastest? Or may be you can advice something else?

Thanks

update: I have even more complex structure now. For example, I need data from Template.parentData(13).
How do you think, can it cause some performance issues?

PS: I don’t want to use Session :slight_smile:


#2

That’s a great question! I’m having the exact same doubt, but I’m having an issue before which is actually getting the parentData to the nested templates.The code runs with no errors but the expected result does not occur.
Can you give you look at this code of mine and tell me what I’m doing incorrectly?

The HTML is

<body>
   {{> A}}
</body>

<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>

And the javascript is:

Template.B.helpers({
  infoMessage: function () {
    return Template.parentData(1);
  }
});

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

#3

Like this…

   Template.A.helpers({
        foo : function () {
            return "bar";
        }
    });

   <template name="A">
        {{> B}}
    </template>
    
    <template name="B">
        {{> C}}
    </template>
    
    <template name="C">
        {{../../foo}}   <!--  is using the relative path to access foo helper from template A -->
    </template>

#4

It’s not working for me. No data is passed to my nested helper. I tested with alerts to see if any data was being passed to the nested template and I get ‘null’ everytime.
Seems to me this is related to this meteor bug with Template.instance(): https://github.com/meteor/meteor/issues/4035
However it seems it works for you two… how exactly is the question.