How to generate HTML with JavaScript?

Spread the love

To generate HTML with JavaScript, we use Mustache.

For instance, we write

const view = {
  url: "/hello",
  name: () => {
    return "Jo" + "hn";
  },
};

const output = Mustache.render(
  '<div><img src="{{url}}" />{{name}}</div>',
  view
);

to call Mustache.render with the Mustache template and the view object with the values for the url and name placeholders.

Leave a Reply

Your email address will not be published. Required fields are marked *