在handlebars中,我需要检查一个对象是否为空,如果不是,则需要运行循环来获取对象的内容。以下是我的代码和codepen链接。我想这很简单,但似乎什么都不起作用。我原本认为handlebars的#if语句会将空对象视为未定义或0,最终条件将无法满足。
<div class="temp"></div>
<script id="temp" type="x-handlebars-template"></script>
var template = Handlebars.compile($("#temp").html());
var tabs = {};
var context = {
tabs: tabs
}
$('.temp').html(template(context));
class="..."
包裹在if
帮助器中。如果不是,请在问题中给出一个例子,说明你期望空对象和非空对象情况下的标记结果分别是什么样的。 - Nick Bartlettif
/unless
helpers和with
helper都将{}
和{a: "test"}
视为“true”。你可以选择:1)创建一个自定义helper;或2)使用别人已经构建的helper,例如http://assemble.io/helpers/helpers-collections.html#-empty-。但是我认为仅使用核心handlebars库是不可能实现这一点的。如果对象不存在,而不是存在且为空,那么这将容易得多。 - Nick Bartlett