javascript - 如何在 Google Apps 脚本中访问 HTML 文件的脚本部分中的模板变量?

标签 javascript html templates google-apps-script

我需要访问 HTML 文件脚本部分中的模板化变量。具体来说,我需要使用 content.price

我可以在 HTML 正文的模板化部分中很好地访问 content.price

这符合预期。
<div class="container">
  Price: $<?= content.price ?></td>
  <div id="paypal-button-container"></div>
</div>

但在脚本部分,尝试访问 content.price 似乎抛出异常。

这会导致按钮无法呈现。 (它会默默地失败。)
amount: {
  value: content.price //  '0.01' // Using content.price throws an error
}

我做错了什么?

代码.gs
var template = HtmlService.createTemplateFromFile(HTML_FILENAME);
template.content = values;
var htmlOutput = template.evaluate();
索引.html
<!DOCTYPE html>
  <!-- source: https://developer.paypal.com/docs/checkout/integrate/ -->
  <html>
    <body>
      <div class="container">
        Price: $<?= content.price ?></td>
        <div id="paypal-button-container"></div>
      </div>
      <script
        src="https://www.paypal.com/sdk/js?client-id=SB_CLIENT_ID">
      </script> 
      <script>
        // This function displays Smart Payment Buttons on your web page.
        paypal.Buttons({
          createOrder: function(data, actions) {
            // This function sets up the details of the transaction, including the amount and line item details.
            return actions.order.create({
              purchase_units: [{
                amount: {
                  value: content.price //  '0.01' // Using content.price throws an error
                }
              }]
            });
          },
        }).render('#paypal-button-container');
      </script>
    </body>
  </html>

最佳答案

在脚本部分,您还需要使用模板标签。

类似这样的东西:

amount: {
  value: <?!= content.price ?>
}

关于javascript - 如何在 Google Apps 脚本中访问 HTML 文件的脚本部分中的模板变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60073188/

相关文章:

javascript - Node 红色 : TypeError: Cannot read property 'nodes' of undefined

css - Bootstrap 3 div 在超大屏幕上

javascript - 在流中传递 ID3 信息并在前端进行解析

c++ - 使用 Clang 10 使用显式模板实例化 ~queue 的 undefined reference

javascript - (Angular) 使用 jQuery(或纯 JS)检查特定的 mat-slide-toggles

javascript - 玛根托 "Miscellaneous scripts"

javascript - 通过单击 JS 驱动的按钮创建 HTML 页面

python - 如何将变量传递给 django 中的所有模板?

按非类型参数类型的 C++ 模板特化

javascript - 如何使用javascript只允许将数字填充到带有类名的输入引用中?