Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get string instead of function? #171

Open
IGx89 opened this issue Nov 1, 2018 · 5 comments
Open

How to get string instead of function? #171

IGx89 opened this issue Nov 1, 2018 · 5 comments

Comments

@IGx89
Copy link

IGx89 commented Nov 1, 2018

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");
@MirekSz
Copy link

MirekSz commented Nov 25, 2018

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

@IGx89
Copy link
Author

IGx89 commented Nov 25, 2018

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 :(

@Stmated
Copy link

Stmated commented Oct 1, 2020

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.

@Baffour
Copy link

Baffour commented Oct 1, 2020

@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!

@Stmated
Copy link

Stmated commented Oct 1, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants