简体   繁体   中英

How to prevent Handlebars to put semicolon in a JS object

I am writing a sprity template and my code generate semi colon:

{{#each layouts}}
{{#each sprites}}

  {{#if dpi}}
    ["@media (-webkit-min-device-pixel-ratio: {{ratio}}), (min-resolution: {{dpi}}dpi)"]: {
  {{/if}}
  {{snakecase (cssesc ../classname)}}: {
  backgroundImage: url('{{escimage url}}'),
  {{#if dpi}}
      backgroundSize: {{baseWidth}}px {{baseHeight}}px,
  {{/if}}
  },
  {{#if dpi}}
      },
  {{/if}}
{{/each}}
{{/each}}

And here the output.

const sprites = {
 icon_00_element_common: {
backgroundImage: url('/sprites/sprite-00_Element_Common.png'),; //I want to stripe this semicolon
}  //And I want no return at the end
, icon_00_element_topbar: {
  backgroundImage: url('/sprites/sprite-00_Element_TopBar.png'),;
}
...

How to well format a plain JS object with Handlebars?

It could maybe be a bit difficult. Handlebars as you might know is better for rendering HTML than Javascript...

I believe Handlebars is inserting this semicolon because it looks like you are outputting an anonymous Javascript function, and such functions need to be terminated.

In general, I would try to get more control of the template by replacing your 3 sets of {{#if dpi}} with just one if/else block, so you can write the two full cases. That would solve the return issue. Then you need to reformat your code somehow to stop Handlebars from thinking you are writing an anonymous function.

See https://github.com/wycats/handlebars.js/commit/25b8e314afa695125ef769283d43831c20c77e21

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM