简体   繁体   English

如何防止把手把分号放在JS对象中

[英]How to prevent Handlebars to put semicolon in a JS object

I am writing a sprity template and my code generate semi colon: 我正在编写一个sprity模板,并且我的代码生成半冒号:

{{#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? 如何使用Handlebars格式化普通的JS对象?

It could maybe be a bit difficult. 这可能有点困难。 Handlebars as you might know is better for rendering HTML than Javascript... 您可能知道,把手比HTML更好地呈现HTML。

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. 我相信Handlebars会插入此分号,因为看起来您正在输出匿名Javascript函数,并且此类函数需要终止。

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. 通常,我会尝试通过仅用一个if / else块替换您的3套{{#if dpi}}来获得对模板的更多控制,因此您可以编写两个完整的案例。 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. 然后,您需要以某种方式重新格式化代码,以阻止Handlebars认为您正在编写匿名函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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