javascript - How to check whether object is empty in handlebars if statement? -
in handlebars need check if object empty , if isn't need run loop grab content object. below code , link codepen. imagine quite simple nothing seems work. have thought handlebars #if statement see empty object undefined or 0 , condition unfulfilled.
<div class="temp"></div> <script id="temp" type="x-handlebars-template"> {{#if tabs}} <p>true</p> {{/if}} </script> var template = handlebars.compile($("#temp").html()); var tabs = {}; var context = { tabs: tabs } $('.temp').html(template(context));
you can use built in handlebars each
helper accomplish you're looking for, , can conditionally render empty object:
var template = handlebars.compile($("#temp").html()); var tabs = {}, test = {a: "resources", b: "contact"}; var context = { tabs: tabs, test: test } $('.temp').html(template(context));
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.0/handlebars.min.js"></script> <div class="temp"></div> <script id="temp" type="x-handlebars-template"> <h3>each helper on empty object</h3> {{#each tabs}} <p>key: {{@key}} value = {{this}}</p> {{else}} <p>your tabs object empty!</p> {{/each}} <h3>each helper on non-empty object</h3> {{#each test}} <p>key: {{@key}} value = {{this}}</p> {{else}} <p>your test object empty!</p> {{/each}} </script>
Comments
Post a Comment