handlebars-loader icon indicating copy to clipboard operation
handlebars-loader copied to clipboard

How to get string instead of function?

Open IGx89 opened this issue 7 years ago • 5 comments

Apologies if the solution is obvious, but is there any way to import Handlebars templates as strings and not functions? I'd like to skip the cost of evaluating the function in my client-side code and also skip the ~60k increase in bundle size from bundling the Handlebars runtime. It seems it should be pretty straightforward, but no matter what options or loader combination I try I haven't been successful.

For example, if I put Hello World into my-template.hbs I'd like the following TypeScript code:

import * as template from './my-template.hbs'
console.log(template);

to produce the following JS code:

console.log("Hello World");

IGx89 avatar Nov 01 '18 17:11 IGx89

In WebPack you can use raw-loader to load content of file "raw-loader!./my-template.hbs"

MirekSz avatar Nov 25 '18 17:11 MirekSz

I wish it were that simple! That was my first of many attempts. Doing so just returns the handlebars function in a string. Looking through this loader's code and comparing it to other loaders, there's no path that outputs just a pure string it looks :(

IGx89 avatar Nov 25 '18 19:11 IGx89

Sorry for resurrecting a dead issue, but did anyone ever figure out a way of doing this? I am trying to use Handlebars to render a template inside Vue, but it keeps just printing the function reference.

Stmated avatar Oct 01 '20 18:10 Stmated

@Stmated this loader spits out template functions, and you need to run them elsewhere to actually get the string generated by the template. Example

Might need to see your code if that doesn't help!

Baffour avatar Oct 01 '20 20:10 Baffour

Yeah, I know how it would usually work. I tried to solve this by using skeleton-loader and eval'ing the content given to it, to return just the resulting html, but that just spits out errors about modules not being found, etc. I guess I'll just go with pug-plain-loader instead, and get back to trying to solve it with handlebars another day.

Stmated avatar Oct 01 '20 20:10 Stmated