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

Mustache.js incompatibility #335

Closed
ghost opened this issue Oct 12, 2012 · 3 comments
Closed

Mustache.js incompatibility #335

ghost opened this issue Oct 12, 2012 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 12, 2012

Assuming these declarations:

var template = '{{#foo}}{{bar}}{{/foo}}';
var view = {
  "foo": {
    "lol": "wut"
  },
  "bar": "Hello World!",
};

I would expect the same output from Handlebars and Mustache. But they are different:

>>> Handlebars.compile(template)(view);
""
>>> Mustache.to_html(template, view);
"Hello World!"
@cadilhac
Copy link

I confirm what you get. I think this is the same issue I added here

@wycats
Copy link
Collaborator

wycats commented Oct 16, 2012

We previously discussed this same issue in #148.

tl;dr: Mustache is violating the Mustache spec (O_O) and Handlebars provides .. to address the same issue that Mustache addresses by inheriting the parent scope.

@bubbatls
Copy link

I've made some code to make hanlebars compatible. it certainbly need improvment but it allows me to detect where the incompatibility appears in my template, and to change progressivly to 'original' handlebars.
Here is the code for those who are interested:

Handlebars.mustacheLookup = function (parent,result,name){
var upNeeded = false;
try{
while(result==null && parent.__parentforlookup!=null){
upNeeded = true;

    parent = parent.__parentforlookup;
    result = parent[name];
  }  
  if(result!=null){
    if(typeof result==='string'){
      return {toString:function(){return result;},__parentforlookup:parent};
    }else{
      result.__parentforlookup = parent;
    }
  }else{
   // console.log('notfound '+name,parent)
  }
  return result; 
}finally{
  if(upNeeded){
    console.log('up',name);
  }
}

};

Handlebars.JavaScriptCompiler.prototype.oldLookup = Handlebars.JavaScriptCompiler.prototype.nameLookup;
Handlebars.JavaScriptCompiler.prototype.nameLookup = function(parent, name , type) {
if(parent.indexOf('depth')===0){
return "Handlebars.mustacheLookup("+parent+","+this.oldLookup (parent, name)+",'"+name+"')";
}else{
return this.oldLookup (parent, name);
}
};

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

No branches or pull requests

3 participants