Closure Templates 生成不使用 StringBuilder 的代码

Closure Templates generates code that does not use StringBuilder

我最近从 GitHub 中提取了一个项目,其中在其 build.xml 中包含一个 compile-soy 任务。该任务使用 SoyToJsSrcCompiler.jar 将几个 soy 文件编译成 javascript。该项目包含目标目录,因此我可以看到编译后的文件包含如下代码:

jive.fbldr.soy.attachments = function(opt_data, opt_sb) {
  var output = opt_sb || new soy.StringBuilder();
  output.append('<div class="fbldr-attachments"><div class="fbldr-attach-head"><p>Use the following form to upload file attachments and, optionally, include a variable to reference the uploaded file in the form\'s HTML source.</p><p>Multiple files may be attached, but only one at a time.  Click "Finished" when all files have been attached.</p></div><div class="fbldr-attach-field"><label>Link to HTML Variable (optional) : </label></div><div class="fbldr-attach-field"><select id="fbldr-attach-link"><option value="" selected="selected">Select HTML variable...</option>');
  var optionList34 = opt_data.variables;
  var optionListLen34 = optionList34.length;

当我 运行 没有任何代码更改的相同任务时,生成的编译结果不断用 opt_ignored 替换 opt_sb 并删除对 soy.StringBuilder 的所有引用。我用运行"java -jar lib/SoyToJsSrcCompiler.jar --outputPathFormat target/soy2/fbldr.soy templates/fbldr.soy"手工代替build.xml。我得到相同的结果。

jive.fbldr.soy.attachments = function(opt_data, opt_ignored) {
  var output = '<div class="fbldr-attachments"><div class="fbldr-attach-head"><p>Use the following form to upload file attachments and, optionally, include a variable to reference the uploaded file in the form\'s HTML source.</p><p>Multiple files may be attached, but only one at a time.  Click "Finished" when all files have been attached.</p></div><div class="fbldr-attach-field"><label>Link to HTML Variable (optional) : </label></div><div class="fbldr-attach-field"><select id="fbldr-attach-link"><option value="" selected="selected">Select HTML variable...</option>';
  var optionList4 = opt_data.variables;
  var optionListLen4 = optionList4.length;

根据我阅读的所有闭包模板文档,预计输出将使用 StringBuilder。我不明白为什么我的调用一直生成忽略 StringBuilder 的输出。有人会碰巧知道是什么原因造成的吗?

StringBuilder 适用于 Internet Explorer 7 和更早版本的浏览器。对于现代浏览器,简单的字符串连接效率更高,关闭模板已更改为使该模式成为默认模式(作为额外的好处,代码更小)。听起来文档尚未更新以反映此更改。

如果出于兼容性原因您需要 StringBuilder,您可以使用 --codeStyle stringbuilder

在命令行上设置此选项