Answer the question
In order to leave comments, you need to log in
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()
}
{{ 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 }}
{{ template "header" }}
<h1>Test</h1>
{{ template "footer"}}
{{ define "footer"}}
its footer
</div>
</body>
</html>
{{ end }}
template: header.html:11:6: executing "header" at <ne .user.Site "">: error calling ne: invalid type for comparison
{{ $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>
Answer the question
In order to leave comments, you need to log in
Pass dot to child templates
{{ template "header" . }}
<h1>Test</h1>
{{ template "footer" . }}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question