Go Nested Templates

Go Nested Templates

We can nest templates in Go, allowing us to create smaller parts of a template, similar to components.

In this example, we will use three files: header.tmpl, body.tmpl, and footer.tmpl, here is the content of the files:

Header

{{ define "Header" }}
Report: {{ .ReportName }} --- Year: {{ .ReportYear }}{{ "\n" }}
------------------------------------------------------------{{ "\n" }}
{{- end }}        

We declare a new template using the {{ define “TemplateName” }} tag and need to close it with {{ end }}.

Footer

{{ define "footer" -}}
------------------------------------------------------------{{ "\n" }}
Report User: {{ .ReportUser }}
{{ end }}        

The footer template is very similar to the header template.

Body

The body template contains the most new information compared to the other templates.

{{ define "body" }}
{{ template "Header" . }}
{{- range .Users -}}
Name:{{ .Name }} LastName: {{ .LastName }}{{ "\n" }}
{{- end -}}
{{ template "footer" . }}
{{ end }}        

Just like we declared the header and footer templates, we also need to declare the body template.

We can use the templates created earlier with the template function, followed by the name of the template we want to use.

Pay close attention to the . after the template name. This . is not mandatory, but by using it, we are passing the data sent to the body template to the header template.

Another change is that the struct passed to the template is now like this:

type Report struct {
	ReportName string
	ReportYear string
	ReportUser string
	Users      []User
}

type User struct {
	Name              string
	LastName          string
	Country           string
	Admin             bool
	YearsOfExperience int
}        

The Report struct will be passed to the template, which is why the range is done on the Users field.

This is our code snippet that executes the template:

type Report struct {
	ReportName string
	ReportYear string
	ReportUser string
	Users      []User
}

type User struct {
	Name              string
	LastName          string
	Country           string
	Admin             bool
	YearsOfExperience int
}

func main() {
	tmplFiles := []string{"header.tmpl", "body.tmpl", "footer.tmpl"}

	users := []User{
		{"John", "Doe", "USA", true, 25},
		{"Gavin", "Steele", "USA", false, 3},
		{"Ashton", "Walsh", "CA", false, 5},
	}

	report := Report{
		ReportName: "Sales",
		ReportYear: "2025",
		ReportUser: "Goku",
		Users:      users,
	}

	tmpl, err := template.New("myReport").ParseFiles(tmplFiles...)
	if err != nil {
		panic(err)
	}

	err = tmpl.ExecuteTemplate(os.Stdout, "body", report)
	if err != nil {
		panic(err)
	}
}        

We need to parse all the files that we referenced in the main body template.

ow we will use the ExecuteTemplate function, passing the standard output, the template that should be loaded since we parsed 3 files, and we also need to pass the data that will be used in the execution.

This is the result:

Report: Sales --- Year: 2025

------------------------------------------------------------
Name:John LastName: Doe
Name:Gavin LastName: Steele
Name:Ashton LastName: Walsh
------------------------------------------------------------

Report User: Goku        






要查看或添加评论,请登录

Daniel M.的更多文章

  • Golang HTTP Requests - Complete Guide

    Golang HTTP Requests - Complete Guide

    HTTP Requests In Go, we can use the net/http package to make http requests. We will use this public endpoint to make a…

  • Using structs with Redis and Golang

    Using structs with Redis and Golang

    Many times, we will need to store a struct in Redis instead of storing only separate fields. We can store a struct like…

  • Redis Hash

    Redis Hash

    A Redis Hash is a data structure in Redis that stores a set of key-value pairs, similar to an object or map in…

  • Go Templates 02 - Conditionals

    Go Templates 02 - Conditionals

    Conditional Templates Another very common feature we need when working with templates is the use of conditions. For all…

  • Go Templates 01 - Basics

    Go Templates 01 - Basics

    Templates Go provides a powerful template system through the text/template and html/template packages, enabling the…

  • Go - sorting slices

    Go - sorting slices

    In Go, sorting a slice is often necessary when working with data that needs to be organized or structured for better…

  • Go - Arrays

    Go - Arrays

    Arrays Array is a data structure used to store a sequence of elements of same type in a fixed-size. An array is stored…

  • Redis SET command.

    Redis SET command.

    Redis set command SET We can set a string value to a key using the set command. In order to check if the value was…

  • Golang - Recursion

    Golang - Recursion

    Recursion The programming language Golang is known for its simplicity, efficiency and for work really well with…

社区洞察

其他会员也浏览了