Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Register an Helper for the Handlebars rendering
//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);
//import
var h = require('handlebars');
h.registerHelper('markdown', require('helper-markdown'));
//-------------------------------------------
//@STCGoal Use Markdown in your Code to Render it as HTML
//Handlebars sources
var code = `
<div>
<h1>{{title}}</h1>
<p>{{description}}</p>
<hr>Using Markdown handlebars-helper<hr>
{{#markdown}}
# Title Something
---
* Points a
* Point b
{{/markdown}}
</div>
`;
//Data structure with values
var data = {
title : "Hello world",
md: `
# My Title
Someting about...
## Sub title
* points
* points 2
`,
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"}]
};
var template = h.compile(code);
console.log(template(data.md));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.