/* Apply an HTML template and render. * * @param templ an HTML template * @tempData usually a struct passed to the template * @w the response writer that should render the template */ func renderTemplateFromFile(context appengine.Context, templ *template.Template, tempData interface{}, w http.ResponseWriter) { err := templ.Execute(w, tempData) if err != nil { serveError(context, w, http.StatusInternalServerError, err) // 500 } return }
func applyTemplate(t *template.Template, name string, data interface{}) []byte { var buf bytes.Buffer if err := t.Execute(data, &buf); err != nil { log.Printf("%s.Execute: %s", name, err) } return buf.Bytes() }
// Escape rewrites each action in the template to guarantee that the output is // HTML-escaped. func Escape(t *template.Template) (*template.Template, os.Error) { c := escapeList(context{}, t.Tree.Root) if c.errStr != "" { return nil, fmt.Errorf("%s:%d: %s", t.Name(), c.errLine, c.errStr) } if c.state != stateText { return nil, fmt.Errorf("%s ends in a non-text context: %v", t.Name(), c) } return t, nil }
// Escape rewrites each action in the template to guarantee that the output is // properly escaped. func Escape(t *template.Template) (*template.Template, os.Error) { var s template.Set s.Add(t) if _, err := EscapeSet(&s, t.Name()); err != nil { return nil, err } // TODO: if s contains cloned dependencies due to self-recursion // cross-context, error out. return t, nil }
func template_exec(tpl *template.Template, request uintptr, k_output uint64, k_input uint64) (err string) { wr, ok2 := f.Hget(request, k_output).(io.Writer) data := f.Hget(request, k_input) if !ok2 { return fmt.Sprintf("templateExecute_handler input data invalid: tpl {%v}; output {%v, %v}; data {%v}", tpl, wr, ok2, data) } if e := tpl.Execute(data, wr); e != nil { return fmt.Sprintf("templateExecute_handler template execution error: %v", e) } return "" }
func render(tmplString string, context interface{}) (string, os.Error) { var tmpl *template.Template var err os.Error if tmpl, err = template.Parse(tmplString, nil); err != nil { return "", err } var buf bytes.Buffer tmpl.Execute(context, &buf) return buf.String(), nil }
func main() { prepareCache() queryString := parseQueryString(os.Getenv("QUERY_STRING")) fmap := template.FormatterMap{"html": template.HTMLFormatter} opts := map[string](interface{}){} os.Stdout.WriteString("Content-type: text/html\n\n") conn := db.PrepareConnection(Cache["db"]) hdr, _ := template.ParseFile(Cache["tpl:headr"], fmap) tmpl := new(template.Template) if queryString["forum"] != "" { tmpl, _ = template.ParseFile(Cache["tpl:forum"], fmap) opts["forum"] = db.Get(conn, db.Forum{}, queryString["forum"]) } else { tmpl, _ = template.ParseFile(Cache["tpl:index"], fmap) } _ = strconv.Itoa c, _ := conf.ReadConfigFile(path.Join(Cache["wd"], "flow.config")) opts["version"] = Version opts["title"], _ = c.GetString("default", "boardName") defer conn.Close() if !db.TablesExist(conn) { db.SetupForum(conn) } stmt, _ := conn.Prepare("SELECT id, name, desc FROM forum;") db.HandleError(stmt.Exec()) forums := []db.Forum{} if stmt.Next() { var f db.Forum db.HandleError(stmt.Scan(&f.Id, &f.Name, &f.Desc)) forums = append(forums, f) } opts["forums"] = forums opts["cwd"] = Cache["cwdir"] hdr.Execute(opts, os.Stdout) tmpl.Execute(opts, os.Stdout) }
func generateRouter() { //mkdir for the initiator err := os.MkdirAll(*TargetFullPath+"/router/", 0755) if err != nil { fmt.Println("Unable to create directory, ", err.String()) } f, err := os.Create(*TargetFullPath + "/router/" + "router.go") if err != nil { fmt.Println(err.String()) } defer f.Close() var templ *template.Template templ = template.New(nil) templ.SetDelims("<%", "%>") err = templ.Parse(routerTemplate) if err != nil { fmt.Println(err.String()) } err = templ.Execute(f, map[string]interface{}{ "PackageName": *PackageName, "ServiceName": *ServiceName, }) if err != nil { fmt.Println(err.String()) } }
func funcToHTML(fset *token.FileSet, t *doce.Func, tpl *template.Template) string { b := bytes.NewBuffer(make([]byte, 0, 128)) recvnostar := t.Recv if recvnostar != "" && recvnostar[0] == '*' { recvnostar = recvnostar[1:] } var data = map[string]string{ "name": t.Name, "code": codeToString(fset, t.Decl), "comment": commentToHTML(t.Doc), "recv": t.Recv, "recvnostar": recvnostar, } tpl.Execute(b, data) return b.String() }
// computeOutCtx takes a template and its start context and computes the output // context while storing any inferences in e. func (e *escaper) computeOutCtx(c context, t *template.Template) context { // Propagate context over the body. c1, ok := e.escapeTemplateBody(c, t) if !ok { // Look for a fixed point by assuming c1 as the output context. if c2, ok2 := e.escapeTemplateBody(c1, t); ok2 { c1, ok = c2, true } // Use c1 as the error context if neither assumption worked. } if !ok && c1.state != stateError { return context{ state: stateError, // TODO: Find the first node with a line in t.Tree.Root err: errorf(ErrOutputContext, 0, "cannot compute output context for template %s", t.Name()), } } return c1 }
// escapeTemplateBody escapes the given template assuming the given output // context, and returns the best guess at the output context and whether the // assumption was correct. func (e *escaper) escapeTemplateBody(c context, t *template.Template) (context, bool) { filter := func(e1 *escaper, c1 context) bool { if c1.state == stateError { // Do not update the input escaper, e. return false } if !e1.called[t.Name()] { // If t is not recursively called, then c1 is an // accurate output context. return true } // c1 is accurate if it matches our assumed output context. return c.eq(c1) } // We need to assume an output context so that recursive template calls // take the fast path out of escapeTree instead of infinitely recursing. // Naively assuming that the input context is the same as the output // works >90% of the time. e.output[t.Name()] = c return e.escapeListConditionally(c, t.Tree.Root, filter) }
/* * Prints out the information given to you by ls.Ls depending on the flags * given * */ func printFiles(numArgs int, data [][]ls.FileData, path string, n *bool, temp *template.Template) { for pos, dir := range data { if pos != 0 { path += "/" path += dir[0].Name fmt.Printf("\n%s:\n", path) } else if numArgs > 1 { fmt.Printf("%s:\n", path) } if *n { fmt.Printf("total %d\n", totalBlocks(dir)) } for pos1, file := range dir { if pos1 != 0 { if *n { temp.Execute(os.Stdout, file) } else { fmt.Println(file.Name) } } } } fmt.Println() }
func run(t *template.Template, a interface{}, out io.Writer) { if err := t.Execute(out, a); err != nil { panic(err) } }
func QR(c *http.Conn, req *http.Request, templ *template.Template) { templ.Execute(req.FormValue("s"), c) }