I have a template data context like this:
data = {
"attribute1": {
"attribute2": {}
}
}
And in the meteor template I am doing something like this:
{{#with attribute1}}
{{#if attribute2}}
show some content
{{/if}}
{{/with}}
I don't want to show anything if attribute2 is an empty object. However I tried both {{#with attribute2}}{{/with}}
and {{#if attribute2}}{{/if}}
And it's still rendering the content inside even if it's an empty object.
What is the correct way to check if the object is empty in spacebar template? Or is it even possible?
I just found a way to register a template helper and use jQuery.isEmpty to do the null check:
Template.registerHelper("isEmpty", function (object) {
return jQuery.isEmpty(object);
});
And to use it in the template:
{{#unless isEmpty attribute2}}
show some content
{{/unless}}
But I found a drawback from this solution that if I want to refer to attributes inside attribute2, I will need to add {{#with attribute2}}{{/with}}
inside the unless block