Mustache / Hogan JS: Can parent tags be listed?

I have a use case in which I would like to access parent tags in sections of a list loop in a JS Mustache / Hogan template.

For example, this is my data structure:

var data = { users: [{name: "John", age: 100},{name: "Max", age: 80}], meta: {membership: "full"} }; 

.. and this is my Mustache / Hogan JS template:

 {{#users}} h1 Hello there, {{name}} {{/users}} 

.. which displays as:

 <h1>Hello there, John</h1> <h1>Hello there, Max</h1> 

This is all good and good, but is it possible for me to access the parent variable meta.membership in {{# users} ... {{/ users}} ? The tags seem to be limited by the local context, so I can not infer the value of the meta.membership tag during iteration over users.

Ideally, I want to know if something like this is possible:

 {{#users}} h1 Hello there, {{name}} p You have a {{meta.membership}} membership {{/users}} 

Desired Result:

 <h1>Hello there, John</h1> <p>You have a full membership</p> <h1>Hello there, Max</h1> <p>You have a full membership</p> 

Thank you in advance

+4
source share
2 answers

PEBKAC !

It turns out that Hogan JS maintains the Bubbling spec context , so my desired input according to the question really evaluates my desired result! :) I just had problems getting this to work as expected, because I was dealing with a heavily nested set data and a few Mustaches, so I made some stupid mistakes along the way that gave me empty exits.

Everything is fine now - although I think that I should go find a Hogan debugger to get rid of further disappointments in the future ...;)

+4
source
 {{#users}} h1 Hello there, {{name}} p You have a {{#meta.membership}} membership {{/users}} 

OR

 {{#users #meta}} h1 Hello there, {{name}} p You have a {{membership}} membership {{/users}} 

Try it ... It can work, because the structure of the data array will allow it to work

-1
source

All Articles