Accessing helpers in content blocks


#1

Am I wrong to think content blocks can be used in this way?

http://meteorpad.com/pad/TNX2uPzeysbWNTPCQ/Leaderboard


#2

You need to understand more fully the idea of which template you are actually in, currently.

And this is easy to do, though not immediately intuitive:

No matter what block helper you are calling, like #if or #myOwnBlockHelper, it doesn’t change what your current template is. Otherwise just calling a single #if would mess up all of your template code.

This means that as long as what you’re writing is contained within e.g.

<template name="myTemplate">
...
</template>

you are always in the (data) context of myTemplate.

<template name="myTemplate">
  Works:
  {{myTemplateVar1}}
  {{#myBlockHelper}}
    Still works:
    {{myTemplateVar1}}
  {{/myBlockHelper}}
</template>

Template.myTemplate.helpers({
  myTemplateVar1: function() { return "myTemplateVar1" })
})

<template name="myBlockHelper">
  This one will not be found:
  {{myTemplateVar1}}
  But this template has its own vars:
  {{myBlockHelperVar1}}
</template>

Template.myBlockHelper.helpers({
  myBlockHelperVar1: function() { return "myBlockHelperVar1" })
})

#3

So in short content blocks don’t affect context at all? Only template does? It helps to understand that “if” is just a block helper and it doesn’t make sense that it changes the data context.

Thanks


#4

Exactly, I think the only block helpers that do change context are with and each. All others keep you in the context of the current template / the one you were in before (or rather outside) the block helper.
And I don’t know if we understood each other about the if block helper – yes, it does not change the data context. Same context outside and inside the if construct.