Last active
April 15, 2019 20:26
-
-
Save jgwill/9466df95697abb495ff63a322075129c to your computer and use it in GitHub Desktop.
Register an Helper for the Handlebars rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import | |
var h = require('handlebars'); | |
//Register an Helper for the Handlebars rendering | |
h.registerHelper('list',function(items,options) | |
{ | |
var out = ` | |
<table> | |
<tr><td>Name</td><td>Note</td></tr>`; | |
items.forEach(element => { | |
out += // Adding | |
` | |
<tr><td>${element.name}</td><td>${element.note}</td></tr>`; | |
}); | |
return `${out} | |
</table>`; //returning the rendering that helper creates | |
}); | |
//Handlebars sources | |
var code = ` | |
<div> | |
<h1>{{title}}</h1> | |
<p>{{description}}</p> | |
<hr>Using a List from handlebar helper<hr> | |
{{#list peoples}}{{/list}} | |
</div> | |
`; | |
//Data structure with values | |
var data = { | |
title : "Hello world", | |
description: "Hello world is a common Magna cillum consequat incididunt laboris nisi voluptate fugiat id voluptate incididunt.", | |
peoples:[{name:"Guillaume",note:"main scientist and artist"},{name:"Peter Pan",note:"ficticious caracter"}] | |
}; | |
//Compile an Handlebar template from the Code | |
var template = h.compile(code); | |
//Creates the HTML Rendering using the handlebars template and the data | |
var htmlRender = template(data); | |
//output | |
console.log(htmlRender); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment