简体   繁体   English

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

[英]How to access template variables in the script section of my HTML file in Google Apps Script?

I need to access templated variables in the script section of my HTML file.我需要在 HTML 文件的脚本部分访问模板化变量。 Specifically, I need to use content.price .具体来说,我需要使用content.price

I can access content.price just fine in the templated section of the body of my HTML.我可以在 HTML 正文的模板部分访问content.price就好了。

This behaves as expected. 这符合预期。
amount: {
  value: content.price //  '0.01' // Using content.price throws an error
}

But in the script section, trying to access content.price seems to be throwing an exception.但是在脚本部分,尝试访问content.price似乎会引发异常。

This causes the buttons to fail to render. 这会导致按钮无法呈现。 (It fails silently.) (它默默地失败了。)
 amount: { value: content.price // '0.01' // Using content.price throws an error }

What am I doing wrong?我究竟做错了什么?

Code.gs 代码.gs
 var template = HtmlService.createTemplateFromFile(HTML_FILENAME); template.content = values; var htmlOutput = template.evaluate();
index.html 索引.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>

In script section also you'll need to use template tags.在脚本部分,您还需要使用模板标签。

Something like these :像这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM