adammark/Markup.js

testing for existence of boolean property

davidh71 opened this issue · 2 comments

Hi, I just started using this library - i'm really liking it compared to the other template libs.
How can you test for the *_existence *_of a property that is a boolean. I must not be using the correct pipes because it thinks the boolean value of false means it doesn't exist.

var context = { name:"bob", thing:false };
var template = "{{if thing|notempty}} the attribute thing exists and has value of {{thing}} {{/if}}";
Mark.up(template, context);

i would expect this to return:
"the attribute thing exists and has value of false"
but it returns:
""

is there anything equivalent to hasOwnProperty ?
Many Thanks

Hi. Perhaps the documentation is unclear, but the "notempty" pipe considers false to be empty.

notempty: function (obj) {
    return obj && (obj + "").trim().length ? obj : false;
},

If you just want to check for the presence of a property, perhaps you could write your own pipe, e.g.

Mark.pipes.hasProperty = function (obj) {
  return obj !== undefined;
};

Let me know if this helps.

That's perfect. It would be a great addition to the default pipes. Thanks for the response!