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

Implement ANCHORED-DOT pragma #482

Open
fish520 opened this issue Sep 9, 2015 · 4 comments
Open

Implement ANCHORED-DOT pragma #482

fish520 opened this issue Sep 9, 2015 · 4 comments

Comments

@fish520
Copy link

fish520 commented Sep 9, 2015

I got mustache today and it amazed me !
but when I'm trying to generate a tree, I went a problem.

this is the code that leads a max stack exceeded error:

var data = [
      {
        nodeName: "1-11111",
        list: [
          {
            nodeName: "2-11111",
            list: [
              {
                nodeName: "3-11111",
                isLeaf: true
              },
              {
                nodeName: "3-22222",
                isLeaf: true
              }
            ]
          }
        ]
      }
    ];
var tpl = '<ul>{{#.}}\n\
  <li>\n\
    <a href="javascript:;">{{nodeName}}</a>\n\
    {{#list}}{{>rec}}{{/list}}\n\
  </li>\n\
{{/.}}</ul>\n';

var output = Mustache.render(tpl, data, {rec: tpl});
console.log(output);

after digging into the source, I found out that it's the context lookup which leads the error.
In my test, the available way is to add a list: [] property to every leaf node, but that's not what the server will return.
how to solve the problem ?

the following syntax is what make it good:

var tpl = '<ul>{{#.}}\n\
  <li>\n\
    <a href="javascript:;">{{nodeName}}</a>\n\
    {{#.list}}{{>rec}}{{/.list}}\n\
  </li>\n\
{{/.}}</ul>\n';

by using .list as a block tagname, it's meaning lookup just the current context, and also, it's reasonable sematically.

as to make it work, just add a few code around line 383: names = name.split('.'), since '.list'.split('.') we get ["", "list"], just remove the empty one and we can get it work !

@bobthecow
Copy link

This should do it for you:

<ul>
  {{#.}}
    <li>
      <a href="javascript:;">{{nodeName}}</a>
      {{^isLeaf}}
        {{#list}}{{>rec}}{{/list}}
      {{/isLeaf}}
    </li>
  {{/.}}
</ul>

The {{.list}} functionality you're asking for was suggested in mustache/spec#52 and implemented by Mustache.php in v2.9.0 as the ANCHORED-DOT pragma.

@fish520
Copy link
Author

fish520 commented Sep 10, 2015

thks for help !

@bobthecow
Copy link

No problem.

@dasilvacontin dasilvacontin changed the title suggestion about how to avoid context lookup Implement ANCHORED-DOT pragma Sep 10, 2015
@dasilvacontin
Copy link
Collaborator

We still haven't started working into having a Plugin API or any kind of support for pragmas, but we'll take ANCHORED-DOT into account once we do so. Thanks for reporting! And thanks @bobthecow!

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

No branches or pull requests

4 participants
@bobthecow @dasilvacontin @fish520 and others