Giannis Spiliopoulos
Giannis Spiliopoulos

Reputation: 2698

templated html returns server error when encountering an undefined variable

Hi i was wondering if this is the expected behaviour:

<html>
  <head>
  </head>
  <body>
    <?if(a){
      Logger.log("a ="+a)?>
      <table>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Id</th>
        </tr>
        <tr>
          <td><input type='text' name=firstName id='firstNameToEdit' /></td>
          <td><input type='text' name=lastName id='lastNameToEdit' /></td>
          <td><label id='down_id'></label></td>
        </tr>
      </table>
    <?}else{Logger.log("a="+a)}?>
  </body>  
</html>

when a is defined i get the value of the a in the log and everythings shows up as expected but when i leave the a undefined the execution seems to stop at if(a) and log shows nothing.

I serve the html like this:

  var t = HtmlService.createTemplateFromFile('test');
  return t.evaluate();

When i serve it this way:

  var t = HtmlService.createTemplateFromFile('test');
  t.a = b;
  return t.evaluate();

where (b:{undefined,null}) then if i log the value of a inside the html template i get that a=null (in both cases). It seems that you can't have an undefined variable inside a template.

P.S. i would really appreciate a way to turn off auto identation in the editor

Upvotes: 0

Views: 235

Answers (1)

Eric Koleda
Eric Koleda

Reputation: 12673

If you run the doGet() method in the editor you should see the error:

ReferenceError: "a" is not defined.

In JavaScript you can't reference a variable that doesn't exist. To work around this you can change the if case to:

<? if (this.a) {

Since this is always defined as the current object/scope.

Upvotes: 1

Related Questions