Skip to content

Instantly share code, notes, and snippets.

@jgwill
Last active April 15, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwill/9466df95697abb495ff63a322075129c to your computer and use it in GitHub Desktop.
Save jgwill/9466df95697abb495ff63a322075129c to your computer and use it in GitHub Desktop.
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