func init_templates() { root := config.Server["templates"] templates["index.html"] = pongo.Must(pongo.FromFile(path.Join(root, "index.html"), nil)) templates["about.html"] = pongo.Must(pongo.FromFile(path.Join(root, "about.html"), nil)) templates["history.html"] = pongo.Must(pongo.FromFile(path.Join(root, "history.html"), nil)) templates["add/page.html"] = pongo.Must(pongo.FromFile(path.Join(root, "add/page.html"), nil)) templates["add/group.html"] = pongo.Must(pongo.FromFile(path.Join(root, "add/group.html"), nil)) }
func (w Wrapper) RenderWithVars(p string, vars TemplateVars) { w.Res.Header().Add("Content-type", "text/html") tplFile := path.Join(w.App.ViewsRoot, p) var tpl *pongo.Template if val, ok := tplCache[tplFile]; ok { tpl = val } else { tpl = pongo.Must(pongo.FromFile(tplFile, nil)) } var context = pongo.Context{ "session": w.Session, } for k, v := range vars { context[k] = v } out, err := tpl.Execute(&context) if err != nil { panic(err) } w.Write(*out) }
func (tpl *DjangoTpl) Display(template string) { var tplExample = pongo.Must(pongo.FromFile(template, nil)) err := tplExample.ExecuteRW(tpl.w, &tpl.ctx) if err != nil { http.Error(tpl.w, err.Error(), http.StatusInternalServerError) } }
/** * Handles request for section */ func handlePaginatedSection(ctx *web.Context, section string, page string) string { config, err := getConfig() if err != nil { ctx.Abort(500, "Configuration error.") return "" } tpl := pongo.Must(pongo.FromFile(config.TemplateFolder+"/template.html", nil)) var content, output string p, _ := strconv.Atoi(page) output, err = getAbstracts(section, p, &config) if err != nil { ctx.Abort(404, "Page not found. Could not load abstracts") return "" } content = string(blackfriday.MarkdownCommon([]byte(output))) menu, err := getMenu(&config) if err != nil { ctx.Abort(501, "Could not load menu") return "" } var response *string response, err = tpl.Execute(&pongo.Context{"content": content, "menu": menu, "currentMenu": menu.GetCurrent(section)}) if err != nil { ctx.Abort(501, "") return err.Error() } return *response }
func include(cfg *config, vars pongo.Context) func(*string) (*string, error) { return func(loc *string) (*string, error) { inc := filepath.ToSlash(filepath.Join(cfg.includes, *loc)) tpl, err := pongo.FromFile(inc, include(cfg, vars)) if err != nil { return nil, err } return tpl.Execute(&vars) } }
func (t *Template) CreateTemplate() (string, error) { tpl, err := pongo.FromFile(t.Source, nil) if err != nil { return "", err } ctx := pongo.Context(t.Context) out, err := tpl.Execute(&ctx) if err != nil { return "", err } return *out, nil }
func renderTemplate(tmpl string, ctx pongo.Context, w http.ResponseWriter) { //err := templates[tmpl].ExecuteRW(w, &ctx) t, err := pongo.FromFile(path.Join(config.Server["templates"], tmpl), nil) //err := templates[tmpl].ExecuteRW(w, &ctx) // Add some general items to context from the config for k, v := range config.Wiki { ctx["_"+k] = v } ctx["_groups"] = get_all_groups() err = t.ExecuteRW(w, &ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
func renderTemplate(tmpl string, ctx pongo.Context, w http.ResponseWriter) { // When in debug, we will load the templates each time so we can modify them // without restarting if configuration.App.Debug { t, err := pongo.FromFile(path.Join(tpl_root, tmpl), nil) err = t.ExecuteRW(w, &ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return } err := templates[tmpl].ExecuteRW(w, &ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
func run_server() error { tpl_root = path.Join(configuration.Templates.Path, "templates/") templates["index.html"] = pongo.Must(pongo.FromFile(path.Join(tpl_root, "index.html"), nil)) svr := fmt.Sprintf("%s:%d", configuration.Server.Bind, configuration.Server.Port) fmt.Printf("Shortly is listening on %s\n", svr) http.HandleFunc("/", root_handler) http.HandleFunc("/api/", api_handler) http.HandleFunc("/app/", app_handler) // Static assets media := path.Join(configuration.Templates.Path, "assets/") http.Handle("/media/", http.StripPrefix("/media/", http.FileServer(http.Dir(media)))) http.ListenAndServe(svr, nil) return nil }
func (te *PongoTemplateEngine) Compile(t string) CompiledTemplate { return PongoTemplate{ tmpl: pongo.Must(pongo.FromFile(t, nil)), } }