Closure Templates生成不使用StringBuilder的代码

时间:2015-02-20 22:07:18

标签: google-closure-compiler google-closure-templates

我最近从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;
&#13;
&#13;
&#13;

当我在没有任何代码更改的情况下运行相同的任务时,生成的编译将继续用opt_ignored替换opt_sb并删除对soy.StringBuilder的所有引用。我跑了&#34; java -jar lib / SoyToJsSrcCompiler.jar --outputPathFormat target / soy2 / fbldr.soy templates / fbldr.soy&#34;手动而不是使用build.xml。我得到了相同的结果。

&#13;
&#13;
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;
&#13;
&#13;
&#13;

从我读过的所有Closure模板文档中,预计输出将使用StringBuilder。我无法弄清楚为什么我的调用不断生成忽略StringBuilder的输出。有人会碰巧知道是什么原因引起的吗?

1 个答案:

答案 0 :(得分:1)

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

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

在命令行上设置此选项