twitter / hogan.js

A compiler for the Mustache templating language
http://twitter.github.io/hogan.js
Apache License 2.0
5.14k stars 431 forks source link

Scope change/issue in 3.0.1 #184

Closed simov closed 10 years ago

simov commented 10 years ago

Hi, I don't follow Hogan's development very closely, but after updating to version 3.0.1 my app broke on this case, and I'm wondering if that's intentional or not?

var fs = require('fs');
var hogan = require('hogan.js');

var template = hogan.compile(fs.readFileSync('template.html', 'utf8'));

var html = template.render({
    arr: [{
        name: 'a',
        check: true,
        sub: {
            check: null
        }
    }, {
        name: 'b',
        check: true,
        sub: {
            check: true
        }
    }]
});

console.log(html);
{{#arr}}
    {{name}}{{#sub}}{{#check}} true{{/check}}{{^check}} false{{/check}}{{/sub}}
{{/arr}}
#2.0.0
a false
b true
#3.0.1
a true
b true
sayrer commented 10 years ago

edit--let me look closer at this.

sayrer commented 10 years ago

Hmm, so our behavior matches Mustache.js but not Mustache.rb. I think I'll treat this as a bug fix and approximate Ruby's behavior by changing our test slightly. We'll treat undefined as a key miss, but null as a hit. We could use the 'in' operator as we did in 2.0, but it is slow.

simov commented 10 years ago

That makes sense, since having null means you intentionally nullifying it. In my case this key is an object, and I always init my objects with null, if I'm not going to add properties directly, but instead assign a whole object later on. So there is no other way to create a negative condition based on that object if I initialize it with {} and if null doesn't work.

edit: + null is part of the json spec, so I might actually want to see that nulls

sayrer commented 10 years ago

See issue #187