Class HtmlTemplate

HtmlTemplate

A template object for dynamically constructing HTML. For more information, see theguide to templates.

Methods

MethodReturn typeBrief description
evaluate()HtmlOutputEvaluates this template and returns anHtmlOutput object.
getCode()StringGenerates a string of JavaScript code, based on the template file, that can be evaluated.
getCodeWithComments()StringGenerates a string of JavaScript code that can be evaluated, with each line of the codecontaining the original line from the template as a comment.
getRawContent()StringReturns the unprocessed content of this template.

Detailed documentation

evaluate()

Evaluates this template and returns anHtmlOutput object. Any properties set on thisHtmlTemplate object will be in scope when evaluating. To debug errors in a template,examine the code using thegetCode() method.

// A template which evaluates to whatever is bound to 'foo'.consttemplate=HtmlService.createTemplate('<?= foo ?>');template.foo='Hello World!';Logger.log(template.evaluate().getContent());// will log 'Hello World!'

Return

HtmlOutput — an HtmlOutput object


getCode()

Generates a string of JavaScript code, based on the template file, that can be evaluated. Thismethod produces a string of JavaScript code based on the template file. Callingeval(<code>) will return a newHtmlOutput object with the content of thetemplate after running all embedded server scripts. The generated code is intended to behuman-readable, and so if you need to debug a template you can callLogger.log(<code>) to see what was produced.

Evaluating this code will implicitly bind in all variables in the current scope. In general,it's preferable to use theevaluate() method, which takes explicit bindings.

consttemplate=HtmlService.createTemplate('<b>The time is &lt;?= new Date() ?&gt;</b>',);Logger.log(template.getCode());

Return

String — a string based on the template, which can be evaluated


getCodeWithComments()

Generates a string of JavaScript code that can be evaluated, with each line of the codecontaining the original line from the template as a comment. This method produces a string ofJavaScript code based on the template file. Callingeval(<code>) will returna newHtmlOutput object with the content of the template after running all embeddedserver scripts. The generated code is intended to be human-readable, and so if you need todebug a template you can callLogger.log(<code>) to see what was produced.

Evaluating this code will implicitly bind in all variables in the current scope. In general,it's preferable to use theevaluate() method, which takes explicit bindings.

consttemplate=HtmlService.createTemplate('<b>The time is &lt;?= new Date() ?&gt;</b>',);Logger.log(template.getCodeWithComments());

Return

String — an string based on the template, which can be evaluated


getRawContent()

Returns the unprocessed content of this template.

consttemplate=HtmlService.createTemplate('<b>The time is &lt;?= new Date() ?&gt;</b>',);Logger.log(template.getRawContent());

Return

String — the template's raw content

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-12-03 UTC.