Пример #1
0
// Generate the static HTML file for the post identified by the index.
func generateFile(td *PostData, idx bool) error {
	var w io.Writer

	// check if template exists
	tplName := td.D["Template"]
	var tpl *template.Template
	var ex bool

	if tpl, ex = postTpls[tplName]; !ex {
		return fmt.Errorf("Template not found: %s", tplName)
	}
	slug := td.D["Slug"]
	fw, err := os.Create(filepath.Join(PublicDir, slug))

	if err != nil {
		return fmt.Errorf("error creating static file %s: %s", slug, err)
	}
	defer fw.Close()

	// If this is the newest file, also save as index.html
	w = fw
	if idx {
		idxw, err := os.Create(filepath.Join(PublicDir, "index.html"))
		if err != nil {
			return fmt.Errorf("error creating static file index.html: %s", err)
		}
		defer idxw.Close()
		w = io.MultiWriter(fw, idxw)
	}
	return tpl.ExecuteTemplate(w, tplName+".amber", td)
}
Пример #2
0
func renderTemplate(w http.ResponseWriter, tName string, tType *template.Template, dataObj interface{}) {

	err := tType.ExecuteTemplate(w, tName+".html", dataObj)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Пример #3
0
func BulkHandler(Layout *template.Template, Exits *Exits) func(http.ResponseWriter, *http.Request) {

	return func(w http.ResponseWriter, r *http.Request) {

		ip := r.URL.Query().Get("ip")
		if net.ParseIP(ip) == nil {
			if err := Layout.ExecuteTemplate(w, "bulk.html", nil); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			return
		}

		port_str := r.URL.Query().Get("port")
		port, err := strconv.Atoi(port_str)
		port_str = "&port=" + port_str
		if err != nil {
			port = 80
			port_str = ""
		}

		str := fmt.Sprintf("# This is a list of all Tor exit nodes that can contact %s on Port %d #\n", ip, port)
		str += fmt.Sprintf("# You can update this list by visiting https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=%s%s #\n", ip, port_str)
		str += fmt.Sprintf("# This file was generated on %v #\n", Exits.UpdateTime.UTC().Format(time.UnixDate))
		str += Exits.Dump(ip, port)
		fmt.Fprintf(w, str)

	}

}
Пример #4
0
func (h *Handler) Apply(w http.ResponseWriter, t *template.Template, tName string, app interface{}) (err error) {
	err = t.ExecuteTemplate(w, tName, app)
	if my, bad := mybad.Check(err, "template execution failure", "name", tName); bad {
		return my
	}
	return nil
}
Пример #5
0
// Render renders the doc to the given writer using the provided template.
func (d *Doc) Render(w io.Writer, t *template.Template) error {
	data := struct {
		*Doc
		Template *template.Template
	}{d, t}
	return t.ExecuteTemplate(w, "root", data)
}
Пример #6
0
func RegistroPrendaGET(html *template.Template) gin.HandlerFunc {
	return func(c *gin.Context) {
		mapa := MapaInfo{}
		mapa.ObtenerDatosRegistroPrenda()
		html.ExecuteTemplate(c.Writer, "registroPrenda.html", mapa)
	}
}
Пример #7
0
func main() {
	var err error
	var tpl *template.Template
	tpl, err = tpl.ParseGlob("templates/*.gohtml")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.Execute(os.Stdout, Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl2.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
}
Пример #8
0
// the index page of the chat
func index(indexTemplate *template.Template, w http.ResponseWriter, r *http.Request) {
	//disable caching: HTTP/1.1 HTTP/1.0 and proxies
	//from http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
	w.Header().Set("Cache-Control", "no-cache,no-store,must-revalidate")
	w.Header().Set("Pragma:", "no-cache")
	w.Header().Set("Expires", "0")
	session, err := sessionStorage.GetSession(w, r)
	if err != nil {
		log.Fatal(err)
	}
	if r.Method == "POST" {
		user := r.FormValue("username")
		session.PutVal("user.name", user)
	}
	_, err = session.GetVal("user.name")
	if err != nil {
		err = indexTemplate.ExecuteTemplate(w, "signup", nil)
		if err != nil {
			log.Fatal(err)
		}
		return
	}
	defaultRoom.mutex.Lock()
	defer defaultRoom.mutex.Unlock()
	err = indexTemplate.ExecuteTemplate(w, "pull_chat", defaultRoom)
	if err != nil {
		log.Fatal(err)
	}
}
Пример #9
0
func (ts *TemplateStore) Render(w http.ResponseWriter, name string, data Model) {
	var tmpl *template.Template
	if ts.Development {
		includes, err := filepath.Glob(ts.TemplateDir + "includes/*.tmpl")
		if err != nil {
			log.Fatal(err)
		}
		files := append(includes, ts.TemplateDir+"layouts/"+name)
		tmpl = template.Must(template.New("func").Funcs(ts.funcs).ParseFiles(files...))
	} else {
		var ok bool
		ts.RLock()
		tmpl, ok = ts.templates[name]
		ts.RUnlock()
		if !ok {
			http.Error(w, "404. Page not found", 404)
			return
		}
	}

	buf := ts.bufpool.get()
	defer ts.bufpool.reset(buf)

	err := tmpl.ExecuteTemplate(buf, "base", data)
	if err != nil {
		log.Println(err)
		http.Error(w, "500. Internal server error", 500)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	buf.WriteTo(w)
	return
}
Пример #10
0
func respondWithTemplate(
	templates *template.Template, templateName string, templateContext interface{}, w http.ResponseWriter,
	r *http.Request) error {

	// you access the cached templates with the defined name, not the filename
	return templates.ExecuteTemplate(w, templateName, templateContext)
}
Пример #11
0
// pageTitle executes "title" template and returns its result or defaultTitle.
func pageTitle(t *html.Template) string {
	b := new(bytes.Buffer)
	if err := t.ExecuteTemplate(b, "title", nil); err != nil || b.Len() == 0 {
		return defaultTitle
	}
	return b.String()
}
Пример #12
0
func RenderDetail(shdarray []*DP, t *template.Template, sh string) error {
	var hisName = time.Now().Format("2006-01-02")
	dshfilename := homeDir + "/changshi/today/detail_" + sh + ".html"
	if _, err := os.Stat(dshfilename); os.IsExist(err) {
		os.Remove(dshfilename)
	}
	dshf, _ := os.Create(dshfilename)
	dshfbw := bufio.NewWriter(dshf)
	err := t.ExecuteTemplate(dshfbw, "detail.tmpl", shdarray)
	if err != nil {
		return err
	}
	dshfbw.Flush()
	dshf.Close()

	dshhfilename := homeDir + "/changshi/his/d_" + sh + "_" + hisName + ".html"
	if _, err := os.Stat(dshhfilename); os.IsExist(err) {
		os.Remove(dshhfilename)
	}
	dshhf, _ := os.Create(dshhfilename)
	dshhfbw := bufio.NewWriter(dshhf)
	err1 := t.ExecuteTemplate(dshhfbw, "detail.tmpl", shdarray)
	if err1 != nil {
		return err
	}
	dshhfbw.Flush()
	dshhf.Close()
	return nil
}
Пример #13
0
/*
Builds a post based on the template
*/
func build_post(ps Post, ptype string) string {
	var doc bytes.Buffer
	var body, name string
	var err error
	var tml *template.Template
	if ptype == "post" {
		tml, err = template.ParseFiles("./templates/post.html", "./templates/base.html")
		name = "./output/posts/" + ps.Slug + ".html"
	} else {
		// This should read the pages template
		tml, err = template.ParseFiles("./templates/page.html", "./templates/base.html")
		name = "./output/pages/" + ps.Slug + ".html"
	}
	err = tml.ExecuteTemplate(&doc, "base", ps)
	if err != nil {
		fmt.Println("Error executing template: ", err)
	}
	body = doc.String()

	f, err := os.Create(name)
	defer f.Close()
	n, err := io.WriteString(f, body)

	if err != nil {
		fmt.Println("Error while writing output: ", n, err)
	}

	return body
}
Пример #14
0
func elemFn(t *htemp.Template, e present.Elem) (htemp.HTML, error) {
	var buf bytes.Buffer
	if err := t.ExecuteTemplate(&buf, e.TemplateName(), e); err != nil {
		return "", err
	}
	return htemp.HTML(buf.Bytes()), nil
}
Пример #15
0
func executeTemplate(t *template.Template, p Page) ([]byte, error) {
	var out bytes.Buffer
	err := t.ExecuteTemplate(&out, "layout", p)
	if err != nil {
		return []byte{}, err
	}
	return out.Bytes(), nil
}
Пример #16
0
func TemplateHTML(writer http.ResponseWriter, tpl *template.Template, name string, params types.M) error {
	writer.Header()["Content-Type"] = []string{ghttp.MIMEHTML + "; charset=utf-8"}
	if len(name) == 0 {
		return tpl.Execute(writer, params)
	}

	return tpl.ExecuteTemplate(writer, name, params)
}
Пример #17
0
func Parse(t *template.Template, name string, data interface{}) string {
	var doc bytes.Buffer
	err := t.ExecuteTemplate(&doc, name, data)
	if err != nil {
		fmt.Println(err)
	}
	return doc.String()
}
Пример #18
0
// Render renders the section to the given writer using the provided template.
func (s *Section) Render(w io.Writer, t *template.Template) error {
	data := struct {
		*Section
		Template    *template.Template
		PlayEnabled bool
	}{s, t, PlayEnabled}
	return t.ExecuteTemplate(w, "section", data)
}
Пример #19
0
func renderTemplate(w http.ResponseWriter, t *template.Template,
	data interface{}) error {

	// set header?
	w.Header().Set("Content-Type", "text/html; charset=utf-8")

	return t.ExecuteTemplate(w, "base", data)
}
Пример #20
0
// execTemplate is a helper to execute a template and return the output as a
// template.HTML value.
func execTemplate(t *template.Template, name string, data interface{}) (template.HTML, error) {
	b := new(bytes.Buffer)
	err := t.ExecuteTemplate(b, name, data)
	if err != nil {
		return "", err
	}
	return template.HTML(b.String()), nil
}
Пример #21
0
func textTemplateString(tmpl *template.Template, name string, obj interface{}) string {
	w := new(bytes.Buffer)
	err := tmpl.ExecuteTemplate(w, name, obj)
	if err != nil {
		panic(err)
	}
	return w.String()
}
Пример #22
0
// Render renders the doc to the given writer using the provided template.
func (d *Doc) Render(w io.Writer, t *template.Template) error {
	data := struct {
		*Doc
		Template    *template.Template
		PlayEnabled bool
	}{d, t, PlayEnabled}
	return t.ExecuteTemplate(w, "root", data)
}
Пример #23
0
// Render a named template name to the HTTP channel
func renderTemplate(w io.Writer, tmpl template.Template, context interface{}) {
	log.Printf("Rendering %s", tmpl.Name())
	err := tmpl.ExecuteTemplate(w, "base.html", context)
	if err != nil {
		log.Printf("ERROR: %s", err)
		w.Write([]byte("Unable to render"))
		return
	}
}
Пример #24
0
Файл: server.go Проект: jf/gwp
func process(w http.ResponseWriter, r *http.Request) {
	rand.Seed(time.Now().Unix())
	var t *template.Template
	if rand.Intn(10) > 5 {
		t, _ = template.ParseFiles("layout.html", "red_hello.html")
	} else {
		t, _ = template.ParseFiles("layout.html", "blue_hello.html")
	}
	t.ExecuteTemplate(w, "layout", "")
}
Пример #25
0
func executeTemplate(t *template.Template, name string, binding interface{}) (*bytes.Buffer, error) {
	buf := new(bytes.Buffer)
	err := t.ExecuteTemplate(buf, name, binding)

	if err != nil {
		return bytes.NewBufferString(err.Error()), err
	}

	return buf, nil
}
Пример #26
0
func (r *TemplateRender) Render(w io.Writer, params map[string]interface{}, temps ...string) (err error) {
	if nil == params {
		params = map[string]interface{}{}
	}

	var tpl *template.Template
	if tpl, err = r.Template(temps...); nil == err {
		err = tpl.ExecuteTemplate(w, temps[len(temps)-1], params)
	}
	return
}
Пример #27
0
func renderWithLayout(t *template.Template, wr io.Writer, name string, data interface{}) error {
	for _, templateName := range []string{"header.html", name, "footer.html"} {
		err := t.ExecuteTemplate(wr, templateName, data)

		if err != nil {
			return err
		}
	}

	return nil
}
Пример #28
0
func renderIndexPage(t *template.Template) error {
	r, w := io.Pipe()
	go func() {
		err := t.ExecuteTemplate(w, "index.html", VCs)
		if err != nil {
			fmt.Println("index.html:", err)
		}
		w.Close()
	}()

	return Put("index.html", r)
}
Пример #29
0
func render(t *template.Template, vc *VC) error {
	r, w := io.Pipe()
	go func() {
		err := t.ExecuteTemplate(w, "vc.html", vc)
		if err != nil {
			fmt.Printf("%s: %s\n", vc.Permalink, err)
		}
		w.Close()
	}()

	return Put("firms/"+vc.Permalink+".html", r)
}
Пример #30
0
func (self *templateEx) execute(tmpl *htmlTpl.Template, data interface{}) string {
	buf := new(bytes.Buffer)
	err := tmpl.ExecuteTemplate(buf, tmpl.Name(), data)
	if err != nil {
		return fmt.Sprintf("Parse %v err: %v", tmpl.Name(), err)
	}
	b, err := ioutil.ReadAll(buf)
	if err != nil {
		return fmt.Sprintf("Parse %v err: %v", tmpl.Name(), err)
	}
	return string(b)
}