Пример #1
0
func handler(res http.ResponseWriter, req *http.Request) {
	var tofu, err = soy.NewBundle().
		AddTemplateFile(os.Args[1]).
		CompileToTofu()
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	var m = make(data.Map)
	for k, v := range req.URL.Query() {
		m[k] = data.String(v[0])
	}

	var buf bytes.Buffer
	err = tofu.Render(&buf, "soyweb.soyweb", m)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	io.Copy(res, &buf)
}
Пример #2
0
	cookie.Value = newSession
	cookie.Path = "/"
	cookie.Expires = time.Now().Add(time.Hour * 24 * 7)
	cookie.HttpOnly = true
	http.SetCookie(c.Writer, &cookie)
}

func SoyMust(b *soy.Bundle) *soyhtml.Tofu {
	t, err := b.CompileToTofu()
	if err != nil {
		panic(err)
	}
	return t
}

var tofu = SoyMust(soy.NewBundle().
	WatchFiles(true).            // watch soy files, reload on changes
	AddTemplateDir("templates")) // load *.soy in all sub-directories)

func RenderSoy(c *gin.Context) {
	var testdata = []data.Map{
		{"names": data.List{}},
		{"names": data.List{data.String("Rob")}},
		{"names": data.List{data.String("Rob"), data.String("Joe")}},
	}
	if err := tofu.Render(c.Writer, "soy.examples.simple.helloNames", testdata[2]); err != nil {
		c.Error(err)
		return
	}
}