P
P
Pavel Rogov2018-08-09 17:26:38
go
Pavel Rogov, 2018-08-09 17:26:38

Why is the data not being passed to another html file?

There is a code for go:

func Render(templateName string, data interface{}) string {
  var templateDir = config.EmailNotice.TemplatesDir
  tmpl := template.New(templateName)

  tmpl, err = tmpl.ParseFiles(
    templateDir + `emails/` + templateName,
    templateDir + `header.html`,
    templateDir + `footer.html`,
  )

  if err != nil {
    log.Println(err)
  }

  var tpl bytes.Buffer
  if err := tmpl.Execute(&tpl, data); err != nil {
    log.Println(err)
  }

  return tpl.String()
}

Which parses three header templates :
{{ define "header" }}

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

{{ $site := "example.com" }}
{{ if ne .user.Site  "" }}
    {{ $site := .user.Site }}
{{ end }}

<div>
    <h1>
        <a href="{{ $site }}">
            <img src="/assets/images/logo.png" alt="logo"/>
        </a>
    </h1>
</div>

{{ end }}

body:
{{ template "header" }}
<h1>Test</h1>
{{ template "footer"}}

footer:
{{ define "footer"}}
     its footer
 </div>
 </body>
 </html>
{{ end }}

I'm passing in the data I want to use in an already rendered template, but it's panicking:
template: header.html:11:6: executing "header" at <ne .user.Site "">: error calling ne: invalid type for comparison

But if this piece:
{{ $site := "example.com" }}
{{ if ne .user.Site  "" }}
    {{ $site := .user.Site }}
{{ end }}

<div>
    <h1>
        <a href="{{ $site }}">
            <img src="/assets/images/logo.png" alt="logo"/>
        </a>
    </h1>
</div>

Transfer to the body template, then everything works. But somehow this is not right.
What am I doing wrong ?
I have already read a lot of things, but everywhere there are only examples of how to use data in only one template

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvelichitel, 2018-08-09
@RogovP

Pass dot to child templates

{{ template "header" . }}
<h1>Test</h1>
{{ template "footer" . }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question