Suppose that I have tow text files (go templates):
child.tmpl
TEXT1
Hello {{ . }}
top.tmpl
TEXT2
{{ template "child.tmpl" "argument"}}
the child.tmpl template is nested in top.tmpl
A typical program to parse them will be :
package main
import (
"os"
"text/template"
)
func main() {
t := template.Must(template.ParseFiles("child.tmpl", "top.tmpl")
t.ExecuteTemplate(os.Stdout, "top.tmpl", nil)
}
Is there any method the pass the template to be embedded in the top-level template as an argument using the {{ . }} notation ?
something like {{ template {{.}} "argument" }}
More generally, what is the best way to define a layout template so I can use it like a top-level template to multiple child templates ?
There are two accepted ways to solve your problem:
The first involves writing your own template-inclusion function and registering it as an template.FuncMap with your template through template.Funcs.
The other way is to use {{define xxx}} blocks in your child templates. Then you could have two different files that define the same template:
file1.html: {{define body}}...{{end}}
file2.html: {{define body}}...{{end}}
Parse the correct file depending on your needs and in your parent template just do {{template body "argument"}}.
In my opinion, the first option is more flexible.
Related
This question already has an answer here:
Golang template engine pipelines
(1 answer)
Closed 4 years ago.
In https://golang.org/pkg/text/template/#hdr-Actions, it has the following explanation
{{template "name" pipeline}} The template with the specified name is
executed with dot set to the value of the pipeline.
what does it mean? what's dot for?
For example, I see the following template code -
{{ define "header" }}
{{ template "top" . }}
{{ template "needs" }}
...
{{ end }}
What is the '.' for following the "top" in the code above?
The value '.' is the current value or cursor as explained in the third paragraph in the documentation:
Annotations in the template refer to elements of the data structure (typically a field of a struct or a key in a map) to control execution and derive values to be displayed. Execution of the template walks the structure and sets the cursor, represented by a period '.' and called "dot", to the value at the current location in the structure as execution proceeds.
The command {{ template "top" . }} executes the template "top" with dot as the argument. Inside "top", dot is set to the argument.
I am new to golang and want to understand how to assign a template variable in golang using the golang variable.
I am using go-swagger to generate go code.
Below is the piece of a customized template to generate my swagger client.
func demo() {
{{range .Operations}}
Value := main.CheckAvail(*{{.Package}}.{{ pascalize .Name }})
{{$value := .Value}}
{{if $value }}
{{ pascalize .Name }}
{{end}}
{{end}}
}
But it gives me the error that:
" <.Value>: can't evaluate field Value in type generator.GenOperation ".
How do I assign the value to the $value? Any help?
This is not possible. The template file is a wire frame for a go file. It just generates a file of go code, which is not executed yet.
All in {{ ... }} is evaluated, when the template is parsed. At this point, all text outside the curly braces is just plain text. After the (successful) execution of the template generation the generated text will be picked up by the go compiler and compiled (if it is correct go code).
In this case you cannot evaluate Value.
I send a slice of articles into template. Each articlestruct is like:
type Article struct {
ID uint32 `db:"id" bson:"id,omitempty"`
Content string `db:"content" bson:"content"`
Author string `db:"author" bson:"author"`
...
}
I can loop over articles slice in a {{range $n := articles}} and get each {{$n.Content}} but what I want is to have only the first one (outside the range loop) to use in headline.
What I tried is:
{{index .articles.Content 0}}
But I get:
Template File Error: template: articles_list.tmpl:14:33: executing
"content" at <.articles.Content>: can't evaluate field Content in type
interface {}
If I just invoke
{{index .articles 0}}
It shows the whole article[0] object.
How can I fix this?
The index function access the nth element of the specified array, so writing
{{ index .articles.Content 0 }}
is essentially trying to write articles.Content[0]
You would want something akin to
{{ with $n := index .articles 0 }}{{ $n.Content }}{{ end }}
A more concise way is:
{{(index .articles.Content 0).Content }}
Which would be the equivalent of articles[0].Content.
{{(index .articles 0).Content}}
I have a question about how to order a map by a slice in Go Templates and if it is possible.
Problem: I have a slice of ordered variable names that I want to display on a website, accompanying them I have a map of metadata of variable information that I would like to display together with the variable.
If I have the following struct that I pass in to the template:
type Data struct {
Variables []string
Information map[string]int
}
I would iterate over the slice and pass the variable name in to the map
{{ range $v := .Variables }} {{ index .Information $v }} {{ end }} // Doesn't work.
Here's a Go Playground with the example.
https://play.golang.org/p/AL2csnXdoU
Question: How can I do this?
I am fairly new to Golang. Thankful for any input.
The following should work. To access .Information inside range, you should use $, which is basically d in your Playground example.
{{ range .Variables }} {{ index $.Information . }} {{ end }}
Is there a way to pass a variable that was iterated over into a Golang/Revel template?
For example, in "header.html", I have
{{range .templates}}
{{template "something" .}}
{{end}}
How can I use current index from the array as an argument to template? I tried embedding another {{.}} as shown in the Revel examples, but that leads to a template compliation error. Would the variable be something like $i?
For example, iterating through in Revel is done like so
{{range .messages}}
<p>{{.}}</p>
{{end}}
However, I read that the . means nil.... how does this work in Revel?
If I understand your question correctly, you can use the range built-in to get the index, and then pass it to the template like this:
{{range $i, $t := .templates}}
{{template "Template.html" $i}}
{{end}}
So if the templates variable was defined like this:
templates := []string{"One", "Two"}
and Template.html contains:
This is from Template.html: {{ . }}<br>
Then the final output will be:
This is from Template.html: 0<br>
This is from Template.html: 1<br>