#Why templates silently fail when executed with properties that were not passed

8 messages · Page 1 of 1 (latest)

ripe helm
#

If I have the following template:

const homeTmpl = `<!DOCTYPE html>
  <html>

  <head>
    <meta charset="UTF-8">
    <title>Go Pratice</title>
  </head>

  <body>
    {{.Error}}
    <h1>From string literal</h1>
    Hello {{.Name}}
  </body>

  </html>`

And I execute it with a struct that doesn't have, for example, Error, something like

type data struct {
  Name string
}

The template will stop the render when it reaches {{.Error}} resulting in a blank page.

There are no errors to be detected.

Why? Is there a way to debug this in more complex templates where the data passed doesn't have all of the required properties/pipelines (whatever those are called)?

fossil solstice
vagrant sluiceBOT
#

      func (t *Template) Execute(wr io.Writer, data any) error
    ```
Execute applies a parsed template to the specified data object, writing the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved.
fossil solstice
#

If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer

#

aer u sure that the error is handled?

ripe helm
#

Yep, classic developer error. I should've noticed it returns an error I can check.

#

How do I tag this as solved? I can only tag as beginner question

fossil solstice
#

use /solved