Kirk Strobeck
Kirk Strobeck

Reputation: 18619

Precompile handlebars partials only

Is there a way to precompile handlebars partials only? I want to leave the variables in tact, but simply assemble the page beforehand.

Here is should the use-case:

  1. Partials get included in a .hbs template
  2. Template can undergo minification/alteration of the HTML with {{foo}} variables in tact.
  3. New .hbs template can be compiled.

Basically, I want to know if there is a way to combine handlebars partials together as a compile step. In other words, only process {{>header}}, don’t process {{name}}

Upvotes: 2

Views: 599

Answers (1)

Kirk Strobeck
Kirk Strobeck

Reputation: 18619

Handlebars.registerPartial('header', 'im the header, hi \\{{name}}');
Handlebars.registerPartial('footer', 'im the footer, hi \\{{name}}');

document.write(Handlebars.compile('{{>header}}im the body, hi \\{{name}}{{>footer}}')({}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.3/handlebars.js"></script>

Upvotes: 2

Related Questions