【问题标题】:Closure Templates generates code that does not use StringBuilder闭包模板生成不使用 StringBuilder 的代码
【发布时间】:2015-02-20 22:07:17
【问题描述】:

我最近从 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_sb 替换为 opt_ignored,并删除对 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 的输出。有人会碰巧知道是什么原因造成的吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 文档肯定是关闭的——当我升级我的 Closure-templates 版本时,我自己也很惊讶。如果您使用较新类型的 @param 注释,soy 编译器会告诉您这一点。
    • @ChadKillingsworth 你介意提交一个关闭模板错误来更新文档吗?
    • 文档好像更新了,只是信息被掩埋了。但是,SoyToJsSrcCompiler 的命令行帮助可能也应该更新。我会为此发出一个拉取请求。
    • 感谢您的帮助,约翰。该选项解决了我的问题,我能够构建和运行我的代码。
    猜你喜欢
    • 2012-03-27
    • 2013-10-03
    • 2014-07-14
    • 2017-06-06
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 2017-07-27
    相关资源
    最近更新 更多