【问题标题】:How to access template variables in the script section of my HTML file in Google Apps Script?如何在 Google Apps 脚本中访问我的 HTML 文件的脚本部分中的模板变量?
【发布时间】:2020-02-05 09:57:56
【问题描述】:

我需要在我的 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>

【问题讨论】:

    标签: javascript html templates google-apps-script


    【解决方案1】:

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

    类似这样的:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多