javascript - How to turn a simple 1-dimensional array into an HTML table using Handlebars.js? -
It seems that it should be simple, but I am having a hell of a time with it.
I have an array with some names in it: var foobar = ["John Doe", "Jane Doe", "Mike Do"];
Then I want to create a simple HTML table using the handlabber JS (yes, I want to include the down arrow between each name):
& lt ; Table & gt; & Lt; TR & gt; & Lt; Td> John Doe & lt; / Td> & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Amp; Darr; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; Td> Jane Do & lt; / Td> & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Amp; Darr; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; Td> Mike Do & lt; / Td> & Lt; / TR & gt; & Lt; / Table & gt; However, I can not find any way to refer to FOB in my template HDBS. {{#foobar}} does not work. I am reading about helpers but I am very confused and there are not many examples of such simple actions.
I know I'm a fool. Start with a template that looks like this:
& Lt; Script id = "template" type = "text / x-handlebars-template" & gt; & Lt; Table & gt; {{#each this}} & lt; Tr & gt; & Lt; TD & gt; {. {}} & Lt; / TD & gt; & Lt; / TR & gt; {{#unless @last}} & lt; Tr & gt; & Lt; TD & gt; & Amp; Darr; & Lt; / TD & gt; & Lt; / TR & gt; {{/ If}} {{/ each}} & lt; / Table & gt; & Lt; / Script & gt; You will pass through the template method from foobar to Handlebars.compile . Then foobar will be referred to as this from within the template, and each recurrence can be referred to with . . I am using jQuery to get the template source HTML and add our results on the page: var foobar = ["John Doe", "Jane Doe", "Mike Do" ]; Var source = $ ('# template'). Html (); Var template = handlabs.com (source); $ ('Body') attached (template (foobar)).
Comments
Post a Comment