google-closure-compilergoogle-closure-templates

Closure Templates generates code that does not use StringBuilder


I recently pull a project from GitHub that included a compile-soy task in its build.xml. The task uses SoyToJsSrcCompiler.jar to compile a couple of soy files into javascript. The project included the target directory so I could see that the compiled files contain code like this:

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;

When I run the same task without any code changes, the resulting compiled keeps replacing opt_sb with opt_ignored and stripped out all references to soy.StringBuilder. I ran "java -jar lib/SoyToJsSrcCompiler.jar --outputPathFormat target/soy2/fbldr.soy templates/fbldr.soy" by hand instead of using the build.xml. I get the same result.

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;

From all the Closure Templates documentation I've read, it is expected the output will use StringBuilder. I cannot figure out why my call keeps generating output that ignores the StringBuilder. Would someone happen to know what could cause this?


Solution

  • StringBuilder was appropriate for Internet Explorer 7 and earlier browsers. For modern browsers simple string concatenation is more efficient and Closure Templates was changed to make that mode the default mode (as an added bonus, the code is smaller). It sounds like the documentation has not been updated to reflect this change.

    If for compatibility reasons your require StringBuilder you can set this option on the command-line using --codeStyle stringbuilder