Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loops, accessing parents properties #174

Closed
ghost opened this issue Oct 15, 2015 · 9 comments
Closed

loops, accessing parents properties #174

ghost opened this issue Oct 15, 2015 · 9 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 15, 2015

Hi, is it possible to access parent object properties within a loop.

For example I have

{
"category": "somenthing",
"products":
[
{name:"Windows",price:100},
{name:"Mac OSX",price:200},
{name:"Ubuntu",price:0}
]
}

then in template, can I access "category" somehow?

{#products}
{$parent.category} {name}, {price} €
{/products}

@edi9999
Copy link
Member

edi9999 commented Oct 15, 2015

Just write {category}.

Explanation : whenever the parser returns undefined, the scope manager will go up to the parent scope. If you write your custom parser, you just have to return undefined if you want to take up to the parent

@edi9999 edi9999 closed this as completed Oct 15, 2015
@ghost
Copy link
Author

ghost commented Oct 15, 2015

thanks it works

@justincy
Copy link

Does this still work with the angular parser?

@edi9999
Copy link
Member

edi9999 commented Aug 30, 2017

Yes, it should also work with angular parser

@justincy
Copy link

You're right, it works. I was running into an issue with filters. It was caused me not returning undefined from custom filters when the input was undefined.

@justincy
Copy link

Is there a way to disabling the behavior of looking up in the parent's scope for a particular tag? I expect the answer to be no but it would be a nice feature.

@edi9999
Copy link
Member

edi9999 commented Aug 30, 2017

If you return "" in angular parser, it will stop looking up the parents. Only null and undefined trigger the parent call.

@justincy
Copy link

justincy commented Aug 30, 2017

That's a good idea. I created a custom tag syntax inspired by mustache/spec#52. Here's how I achieved it. It's a modification of the custom angular parser.

    get: tag === '.' ? function(s){ return s;} : function(s) {

      // Allow for a preceding . to indicate that scope should be restricted the current scope
      // and not travel up the parent scopes. This is achieved by returning '' instead of undefined.
      // https://github.com/open-xml-templating/docxtemplater/issues/174
      var restrict = false;
      if(tag[0] === '.') {
        tag = tag.substring(1);
        restrict = true;
      }

      var result = expressions.compile(tag.replace(/|/g, "'"))(s);
      if(result === undefined && restrict) {
        return '';
      } else {
        return result;
      }
    }

@edi9999
Copy link
Member

edi9999 commented Aug 30, 2017

Great !

I would check with == undefined (two equals), because that will also match null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants