1. Variable assignment
A variable can be initialized in the Action to capture the execution result of the pipeline. The initialization syntax is as follows:
where $variable is the name of the variable. Declaring a variable's action will not produce any output.
{{$variable := pipeline}}
2、range
Looping slice array, etc.
If the "range" action initializes 1 variable, the variable is set to each member element of the iterator, if the 2 comma-separated variables are initialized:
At this time, key and val are set as the index of the array/slice or the key of the dictionary, and the corresponding member elements respectively. Note that this is different from the go range clause when setting it to index/key when there is only one parameter!
// Here val is every element, not an index{{range $val := .data}} {{end}} {{range $key,$val := .data}} {{end}}
A variable's scope only ends with the "end" of its control structure ("if", "with", "range"). If it is not declared in the control structure, it will be until the template ends. A call to a subtemplate does not inherit a variable from the location (scope) where it was called.
When the template starts executing, $ will be set to the parameter passed to the Execute method, that is, the initial value of dot.
3. Output the original string
Below are some single-line templates showing pipelines and variables. All the quoted words "output" are generated:
{{"\"output\""}} // String constants{{`"output"`}} // Original string constant{{printf "%q" "output"}} // Function call{{"output" | printf "%q"}} // Function call, the last parameter comes from the return value of the previous command{{printf "%q" (print "out" "put")}} // Parameters with brackets{{"put" | printf "%s%s" "out" | printf "%q"}} // Chain call of the pipe that plays out of flowers{{"output" | printf "%s" | printf "%q"}} // Chain call of pipeline{{with "output"}}{{printf "%q" .}}{{end}} // Use dot with action{{with $x := "output" | printf "%q"}}{{$x}}{{end}} //Create and use variables with action{{with $x := "output"}}{{printf "%q" $x}}{{end}} // Use variables in another action with action{{with $x := "output"}}{{$x | printf "%q"}}{{end}} // Use variables in pipe form on anotheractionofwith action
4. Add template custom method
Custom methods must be added before ParseFiles parsing, because the methods and syntax in the template file will be checked at the same time during parsing.
There are several ways to initialize:
1. Initialize empty template
If the initialization of an empty template, the ExecuteTemplate method must be called during execution, and the parsed template name must be specified (as long as the template name is enough)
t := ("") t = ({"formatStr": formatStr}) t, err = ("view/") err = (w, "", p)
2. Initialize the parsing template
If the initialization of the parsing template, you can call the ExecuteTemplate method during execution, and specify the parsed template name (as long as the template name is enough) or the Execute method are OK
t := ("") t = ({"formatStr": formatStr}) t, err = ("view/") //err = (w, "", p) // orerr = (w, p)
3. Custom method for nested templates
Generally speaking, pages refer to multi-template nesting display: so in this case, the template names in New and ExecuteTemplate need to be the most parent template file, as shown below
t := ("") t = ({"formatStr": formatStr}) t, err = ([]string{ "view/", "view/", "view/", "view/", }...) //err = (w, "", p) // orerr = (w, p)
This is the end of this article about detailed explanation of the common grammar of templates in golang. For more related golang template content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!