Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Handlebars.js in Angular 6+ CLI?

I would like to render a basic customer letter either in plain text format or HTML format so the customer could further edit in TextArea or a HTML Editor. I am thinking of using Handlebars.js to render a HTML template with mustache tags along with a JSON object containing various business info.

I had done:

import * as Handlebars from 'handlebars';

...
            const template = Handlebars.compile(this.exampleHtmlTemplate);
            this.note = template({ title: 'My title', body: 'My body' });

When compiling with Angular Cli, I got the following error:

chunk {vendor} vendor.js, vendor.js.map (vendor) 7.84 MB [initial] [rendered]

WARNING in ./node_modules/handlebars/lib/index.js 22:38-56
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./node_modules/handlebars/lib/index.js 23:2-20
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./node_modules/handlebars/lib/index.js 24:2-20
require.extensions is not supported by webpack. Use a loader instead.

ERROR in ./node_modules/handlebars/lib/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\...\NGSource\node_modules\handlebars\lib'

I am using Angular 7.2.6 and Angular CLI 7.3.3. How to make Handlebars.js work with Angular2+? Or, is it possible to use some low level Angular API to render/compile a template with mustache along with JSON data into a standard HTML/Text string ?

like image 764
ZZZ Avatar asked Mar 22 '19 04:03

ZZZ


People also ask

Can you use JavaScript in handlebars?

Handlebars compiles templates into JavaScript functions. This makes the template execution faster than most other template engines.

Does AngularJS use handlebars?

Handlebars and Angular are completely different things. Handlebars is a template engine. You write a fancy templatey-string, give it a JSON object, and it renders out HTML from the data. There's no data-binding, no updating, it's just a once-off render.

How do I use handlebars in node js?

Here's what's going on, Handlebars takes a static template file you give it inside the layouts folder (the “index. handlebars” file), this file contains some kind of a placeholder (the {{{body}}} if you look back at the code) which will be filled with the content of another . handlebars file (“main. handlebars”).


1 Answers

You can try to use different import:

import * as Handlebars from 'handlebars/dist/cjs/handlebars';
like image 116
Timofey Orischenko Avatar answered Sep 22 '22 23:09

Timofey Orischenko