M
M
MaxRumata2016-08-11 00:35:57
HTML
MaxRumata, 2016-08-11 00:35:57

How to populate HTML with content from JSON via mustache/handlebars?

I need the site's HTML to use mustache or handlebars to populate itself with content from a JSON file.
In search of lessons, I found an interesting service for testing such a bundle with handlebars: tryhandlebarsjs.com
What files should the bundle consist of and how exactly can Handlebars, JSON and HTML be linked together to achieve this effect?
I would be grateful for the clarification, as there is a lot of new for me in this approach.
Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yogurt1, 2016-08-11
@MaxRumata

The Handlebars library receives a template as input and returns a
function This function takes data as input (a JS object that can be obtained from JSON using the JSON.parse function)
The result of the function is an HTML code that you shove wherever you want through innerHTML
Template can be embedded in HTML via the script tag, specifying the desired ID, which you will then pass to
handlebars

fetch('/api/jsondata')
  .then(res => res.json())
  .then(data => el.innerHTML(Handlebars.compile(data)))
  .catch(err => throw err)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question