用于动态构建 HTML 的模板对象。如需了解详情,请参阅模板指南。
方法
方法 | 返回类型 | 简介 |
---|---|---|
evaluate() | HtmlOutput | 评估此模板并返回 HtmlOutput 对象。 |
getCode() | String | 根据模板文件生成可评估的 JavaScript 代码字符串。 |
getCodeWithComments() | String | 生成可求值的一组 JavaScript 代码,其中的每行代码都包含模板中的原始行作为注释。 |
getRawContent() | String | 返回此模板的未处理内容。 |
详细文档
evaluate()
评估此模板并返回 HtmlOutput
对象。评估时,此 HtmlTemplate
对象上设置的任何属性都将在范围内。如需调试模板中的错误,请使用 getCode()
方法检查代码。
// A template which evaluates to whatever is bound to 'foo'. var template = HtmlService.createTemplate('<?= foo ?>'); template.foo = 'Hello World!'; Logger.log(template.evaluate().getContent()); // will log 'Hello World!'
返回
HtmlOutput
- 某个 OutputOutput 对象
getCode()
根据模板文件生成可评估的 JavaScript 代码字符串。此方法会根据模板文件生成一个 JavaScript 代码字符串。运行所有嵌入式服务器脚本后,调用
eval(<code>)
将返回包含模板内容的新 HtmlOutput
对象。生成的代码应该是人类可读的,因此如果您需要调试模板,可以调用
Logger.log(<code>)
来查看生成的代码。
评估此代码将隐式绑定在当前范围内的所有变量中。一般而言,最好使用 evaluate()
方法,该方法接受显式绑定。
var template = HtmlService.createTemplate('<b>The time is <?= new Date() ?></b>'); Logger.log(template.getCode());
返回
String
- 基于模板的字符串,可以求值
getCodeWithComments()
生成可求值的一组 JavaScript 代码,其中的每行代码都包含模板中的原始行作为注释。此方法会根据模板文件生成一串 JavaScript 代码。运行所有嵌入式服务器脚本后,调用 eval(<code>)
将返回包含模板内容的新 HtmlOutput
对象。生成的代码具有人类可读性,因此如果您需要调试模板,可以调用 Logger.log(<code>)
来查看生成的代码。
评估此代码将隐式绑定在当前范围内的所有变量中。一般而言,最好使用 evaluate()
方法,该方法接受显式绑定。
var template = HtmlService.createTemplate('<b>The time is <?= new Date() ?></b>'); Logger.log(template.getCodeWithComments());
返回
String
- 基于模板的字符串,可以求值
getRawContent()
返回此模板的未处理内容。
var template = HtmlService.createTemplate('<b>The time is <?= new Date() ?></b>'); Logger.log(template.getRawContent());
返回
String
- 模板的原始内容