Example #1
0
func (s Server) initTemplates() {
	server.Templates = make(map[string]Template)
	if !fileExist(getTemplateName("error")) {
		tmp, _ := template.Parse(htmlError, nil)
		server.Templates["error"] = tmp
	}
	if !fileExist(getTemplateName("notfound")) {
		tmp, _ := template.Parse(htmlNotFound, nil)
		server.Templates["notfound"] = tmp
	}
}
Example #2
0
func analytics(a urlshortener.AnalyticsInfo) {
	var b bytes.Buffer
	tmpl, _ := template.Parse(`
Kind: {Kind}
Id: {Id}
LongUrl: {LongUrl}
Status: {Status}
Created: {Created}
AllTime:
{.section Analytics.AllTime}
  ShortUrlClicks: {ShortUrlClicks}
  LongUrlClicks: {LongUrlClicks}
  Referrers:
{.repeated section Referrers}    {Id} ({Count})
{.end}
  Countries:
{.repeated section Countries}    {Id} ({Count})
{.end}
  Browsers:
{.repeated section Browsers}    {Id} ({Count})
{.end}
  Platforms:
{.repeated section Platforms}    {Id} ({Count})
{.end}
{.end}
`, nil)
	tmpl.Execute(a, &b)
	println(b.String())
}
Example #3
0
func myWidgets(w http.ResponseWriter, r *http.Request) {
	var err os.Error
	ctx := appengine.NewContext(r)

	page, err := template.Parse(myWidgetTemplate, nil)
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	data := myWidgetData{
		CSS:    commonCSS(),
		Header: header(ctx),
	}

	data.Widget, err = LoadWidgets(ctx)
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}

	if len(r.FormValue("ids_only")) > 0 {
		w.Header().Set("Content-Type", "text/plain")
		for _, widget := range data.Widget {
			fmt.Fprintln(w, widget.ID)
		}
		return
	}

	page.Execute(w, data)
}
Example #4
0
func template_load(backend uintptr, request uintptr) (tpl *template.Template, err string) {
	var tpl_str []byte

	// request size
	q_count := f.Hash([]f.Hskel{
		f.Hitem(f.HK_action, f.TYPE_UINT32T, f.ACTION_COUNT),
		f.Hitem(f.HK_buffer, f.TYPE_SIZET, 0),
		f.Hnext(request)})
	f.Backend_pass(backend, q_count)

	str_len, ok := f.Hget(q_count, f.HK_buffer).(uint)
	if !ok || str_len == 0 {
		tpl_str = []byte("")
	} else {
		tpl_str = make([]byte, str_len)

		// read all
		q_read := f.Hash([]f.Hskel{
			f.Hitem(f.HK_action, f.TYPE_UINT32T, f.ACTION_READ),
			f.Hitem(f.HK_buffer, f.TYPE_RAWT, tpl_str),
			f.Hnext(request)})

		f.Backend_pass(backend, q_read)
	}

	tpl, e := template.Parse(string(tpl_str), nil) // TODO BAD BAD BAD
	if e != nil {
		err := fmt.Sprintf("templateLoad_handler template parse error: %v", e)
		return nil, err
	}
	return tpl, ""
}
Example #5
0
func handleFriendList(writer http.ResponseWriter, request *http.Request) {
	if request.RawURL == "/favicon.ico" {
		return
	}

	var (
		friends []friend
		err     os.Error
	)

	authorizeUrl := AUTHORIZE_URL_BASE + config["client_id"]
	parts := strings.Split(request.RawURL, "?code=", -1)
	if 2 <= len(parts) {
		if err = authorizeCode(parts[1]); err != nil {
			redirect(writer, request, authorizeUrl)
		} else {
			redirect(writer, request, "/")
		}
		return
	} else if currentTokens == nil {
		redirect(writer, request, authorizeUrl)
		return
	}

	if friends, err = getFriendList(); err != nil {
		redirect(writer, request, authorizeUrl)
	} else {
		params := new(struct{ Friends []friend })
		params.Friends = friends
		tmpl, _ := template.Parse(TEMPLATE, nil)
		tmpl.Execute(writer, params)
	}
}
Example #6
0
func Dispatch(w http.ResponseWriter) os.Error {
	t := View.Themes.Current()
	if t == nil {
		se := &ServerError{404, "Not Found"}
		se.Write(w)
		return os.ENOENT
	}
	w.SetHeader("Content-Type", "text/html; charset=utf-8")
	w.SetHeader("Content-Encoding", "gzip")

	templ, err := template.Parse(t.Index, nil)
	if err != nil {
		return err
	}
	gz, err := gzip.NewWriter(w)
	if err != nil {
		return err
	}
	err = templ.Execute(gz, View)
	if err != nil {
		return err
	}

	gz.Close()
	return nil
}
Example #7
0
func loadTmpl(tdir, name string) (tmpl *template.Template, err os.Error) {
	path := gopath.Join(tdir, name)
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return
	}
	return template.Parse(string(data), nil)
}
Example #8
0
func ReadTemplate(templateDir, name string) *template.Template {
	log.Println("  Reading template ", name)
	templatePath := path.Join(templateDir, name)
	templateText := readFile(templatePath)
	template, err := template.Parse(templateText, nil)
	if err != nil {
		panic(err)
	}
	return template
}
Example #9
0
func readTemplate(name string) *template.Template {
	path := pathutil.Join(*tmplroot, name)
	data, err := ioutil.ReadFile(path)
	if err != nil {
		log.Exitf("ReadFile %s: %v", path, err)
	}
	t, err := template.Parse(string(data), fmap)
	if err != nil {
		log.Exitf("%s: %v", name, err)
	}
	return t
}
Example #10
0
func readTemplate(name string) *template.Template {
	fileName := path.Join(*root, "tmpl", name)
	data, err := ioutil.ReadFile(fileName)
	if err != nil {
		log.Exitf("ReadFile %s: %v", fileName, err)
	}
	t, err := template.Parse(string(data), fmap)
	if err != nil {
		log.Exitf("%s: %v", fileName, err)
	}
	return t
}
Example #11
0
func Update(b []byte) (f *bytes.Buffer) {
	templ, _ := template.Parse(templatestr, fmap)
	/*
		templ.rdelim = []byte{ '}', '}'}
		templ.ldelim = []byte{ '{', '{'}
	*/
	var table mytable
	f = bytes.NewBuffer(nil)
	table.v = b
	templ.Execute(table, f)
	return
}
Example #12
0
File: web.go Project: tjweir/web.go
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
}
Example #13
0
func header(ctx appengine.Context) string {
	page, err := template.Parse(headerTemplate, nil)
	if err != nil {
		return fmt.Sprintf("<b>Error</b>: %s<br/>", err)
	}

	data := &headerData{
		User: user.Current(ctx),
	}

	buf := bytes.NewBuffer(nil)
	err = page.Execute(buf, data)
	if err != nil {
		return fmt.Sprintf("<b>Error</b>: %s<br/>", err)
	}

	return buf.String()
}
Example #14
0
File: godoc.go Project: ssrl/go
func readTemplate(name string) *template.Template {
	path := filepath.Join(*goroot, "lib", "godoc", name)
	if *templateDir != "" {
		defaultpath := path
		path = filepath.Join(*templateDir, name)
		if _, err := fs.Stat(path); err != nil {
			log.Print("readTemplate:", err)
			path = defaultpath
		}
	}
	data, err := fs.ReadFile(path)
	if err != nil {
		log.Fatalf("ReadFile %s: %v", path, err)
	}
	t, err := template.Parse(string(data), fmap)
	if err != nil {
		log.Fatalf("%s: %v", name, err)
	}
	return t
}