There was a CL that added this kind of functionality
(http://codereview.appspot.com/4671056/) but
it was reverted soon afterwards by 4700043.
I'm not entirely sure why. Perhaps it's a security
hole, or thwarts potential static analysis.
this approach could work fine. how about something like this, which
defines a new function to execute a dynamic template value.
package main
import (
"fmt"
"os"
"exp/template"
"bytes"
)
func RunTemplate(x interface{}, t *template.Template) (string, os.Error) {
var buf bytes.Buffer
err := t.Execute(&buf, x)
return buf.String(), err
}
func main() {
x := template.New("root").Funcs(template.FuncMap{"runtemplate":
RunTemplate}).
MustParse(`{{.Foo | runtemplate "first"}} {{.Bar |
runtemplate "second"}}`)
x1 := template.New("x1").MustParse(`x1{{.}}`)
x2 := template.New("x2").MustParse(`{{.}}x2`)
err := x.Execute(os.Stdout, struct{Bar, Foo *template.Template}{x2, x1})
if err != nil {
fmt.Printf("error on execute: %v\n", err)
}
}
{{with $t <some pipleline to .Tmpl>}}{{template $t .}}{{ end }} This requires CL 270e8bf51d35dd80f91be48efcd4f597fb639f85. |
Correct. We want the template language to be statically analyzable so the context of a template's invocation is clear, checkable, and lockdownable. If an invocation point is totally dynamic, this can't be done. Similarly, if a template can belong to multiple sets, its context can differ between sets in a way that would require all sets to be analyzed simultaneously. Since both these constraints are easy to work around if you want to, at the cost of losing those static checks in a higher-level package, it seemed wise to control the situation in the base template implementation. A higher-level package, such as a hypothetical HTML-only wrapper, can guarantee no workarounds more easily if the constraints are clear.
I am still learning.
-rob