-2

This is my first stab at doing front end with Go. I have this template:

{{define "index"}}{{template "_header.html"}}
{{template "_message.html"}}
<div>Page Index</div>
<div>Welcome {{.Title}}</div>    
{{template "_footer.html"}}     
{{end}}

Where the {{.Title}} works as expected.

But when I attempt to use it in an include file {template "_header.html"} it fails.

{{define "_header.html"}}<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"/>
        <link rel="stylesheet" href="./css/css.css"/>
        <title>{{.Title}}</title>
    </head><body>{{end}} 

Now the <title> tag is empty.

Any advice?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
pigfox
  • 1,301
  • 3
  • 28
  • 52

1 Answers1

2

According to the template documentation the use of {{ template "name" }} passes nil data to your template. Thus, there is no data to render into {{ .Title }}. You need to use {{ template "name" pipeline }} to pass your data into the included template.

In your case {{ template "_header.html" . }} should do the trick, by passing the current data object into your included template.

See template documentation here:

{{template "name"}}
    The template with the specified name is executed with nil data.

{{template "name" pipeline}}
    The template with the specified name is executed with dot set
    to the value of the pipeline.
pscheid
  • 450
  • 4
  • 10