Template Arguments Getting Rid of Data Context


#1

I am including a template with the usual {{> someTemplate}}. This works perfectly and I access the relevant data in that template.

Now I am attempting to add an argument to the include, like so {{> someTemplate action="update"}}. And when I do so, the data just goes missing because it no longer appears in the rendered HTML. On the off chance that action is a keyword, I even tried {{> someTemplate abcdefg="update"}} and the data still doesn’t show up.

Any ideas as to what the issue is?


#2

action="update" sets up a new context, so the former one is not directly accessible anymore.
You can still access it using the ../ syntax in html, or Template.parentData(n) in js.
You can also put the former context back into the new one:

  {{> someTemplate formerContext=this action="update"}}

#3

Thank you for the reply. I ended up extending the parent context with a StackOverflow solution. I find it odd that I have to do this myself or use parent accessors to get that data. All I’ve changed is one thing. It shouldn’t overwrite unless I specify as much. Can someone from MDG explain why overwriting is the default case?

Template.registerHelper('extendContext', function(data) {
  var result = _.clone(this);
  _.each(data.hash, function(value, key) {
    result[key] = value;
  })
  return result;
});

#4

If overwriting was not the default, someone would have asked: "can someone from MDG explain why the parent context is still there?! It breaks the isolation principle! When I call a function, I don’t get all arguments of the function’s caller! "

The fact is this whole “data context” thing was a bad idea. I think (and hope) MDG will remove it in the future, as discussed here: https://meteor.hackpad.com/Proposal-for-Blaze-2-bRAxvfDzCVv.


#5

You can set the data context like this. You can either use Template.dynamic and our Blaze+ projects.
See more info: Blaze Plus