0

我最近从 GitHub 中提取了一个项目,该项目在其 build.xml 中包含了一个 compile-soy 任务。该任务使用 SoyToJsSrcCompiler.jar 将几个大豆文件编译成 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 的输出。有人会碰巧知道是什么原因造成的吗?

4

1 回答 1

1

StringBuilder 适用于 Internet Explorer 7 和更早的浏览器。对于现代浏览器,简单的字符串连接更有效,并且更改了闭包模板以使该模式成为默认模式(作为额外的奖励,代码更小)。听起来文档尚未更新以反映此更改。

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

于 2015-02-21T03:04:36.003 回答