As we scale our website, we will likely be embedding a html template into another template so that we can reuse code and content.
This example demonstrates 2 things:
- Parse and load multiple templates.
- Pass data to an embedded template.
Multiple templates can be parsed in one of the following ways:
- Use both
filepath.Glob
andtemplate.ParseFiles
- Use
template.ParseGlob
, which is a convenience function of calling the above 2 functions.
Pass the return value from the above function call to template.Must
, which will return a template.Template
value.
We use the ExecuteTemplate
method in the template.Template
value to render a specific named template. To make this work, we must define a template name in the template file *.gohtml
with the directive {{define "myTemplateName"}}
.
If you look at the code, you see that we embed a template named content
into the home
template by using this directive {{template "content" .Data}}
. This directive tells Go runtime to render the content from the template content
along with the passed parametric data .Data
onto the parent template home
. The problem is that we can only pass 0 or 1 parameter to the template. So if you need to pass more than 1 value, you would need to wrap all the multiple values as a complex data type.
-
Run the server.
$ make run
-
Launch a web browser and navigate to http://localhost:8000.