0

This is my handlebar block in html layout:

{{json language}}

This is my handlebar helper in my main build.js:

  handlebars.registerHelper('json', function(language) {
    var data = {
      "marathi" : "m",
      "hindi" : "h",
      "english" : "e"
    }
    return ??
  }); 

How do I return the "data" to the same html layout above? It can go anywhere in the html. The "language" comes from a yaml frontmatter in markdown file. If possible I want to loop through a specific json depending on the value of "language".

Thanks!

xpark
  • 51
  • 1
  • 11

1 Answers1

0

Here is an example (jsfiddle):

// In your HTML page:
// <script id="my-template" type="text/x-handlebars-template">{{json date}}</script>
// <output></output>

// Define the helper
Handlebars.registerHelper('json', function(context, options) {
  return JSON.stringify(context, null, 2);
});

// Usage
var source = document.querySelector("#my-template").innerHTML;
var template = Handlebars.compile(source);
var html = template({
  title: 'Lorem Ipsum Dolor',
  date: {month: 'January', day: 13, year: 2012}
});

document.querySelector('output').innerHTML += '<pre>'+ html +'</pre>';
  • It shows the data in "my-template" but nothing shows up in . I put your code in the script. I even put the helper in my node.js build file – xpark Sep 17 '16 at 11:40