javascript - Handlebars each from a custom start point -
say have object,
{ "empid": "1002729041", "empname": "abhijit audhya" }, { "empid": "1004563331", "empname": "abdul mulla" }, { "empid": "1004703190", "empname": "abdul razic" }, { "empid": "1004912437", "empname": "abdul hafeez" }
i can use handlebars loop through entire object using,
{{#each myobj}} <li>{{empname}}</li> {{/each}}
say, want start 3rd object, ignoring 1st , 2nd. there simple way achieve this?
no, need helper that: logical operator in handlebars.js {{#if}} conditional
so if index > 2 show content
.
if may ask: why not drop first 2 (or matter: unneeded elements) in actual javascript before calling template?
edit: here are...
handlebars.registerhelper('eachfrom', function(context, count, options) { var ret = ""; context.slice(count).foreach(function(elem) { ret += options.fn(elem); }); return ret; }); var context = { myobj: [{ "empid": "1002729041", "empname": "abhijit audhya" }, { "empid": "1004563331", "empname": "abdul mulla" }, { "empid": "1004703190", "empname": "abdul razic" }, { "empid": "1004912437", "empname": "abdul hafeez" }] }; var source = $("#entry-template").html(); var template = handlebars.compile(source); $("#container").append(template(context));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.jsdelivr.net/handlebarsjs/3.0.3/handlebars.min.js"></script> <script id="entry-template" type="text/x-handlebars-template"> <ul> {{#eachfrom myobj 3}} <li>{{empname}}</li> {{/eachfrom}} </ul> </script> <div id="container"></div>
Comments
Post a Comment