Esempio n. 1
0
func main() {
	app := f.CreateApp()
	app.Engine(".html", mustache.Create())
	app.Get("/", func(req *f.Request, res *f.Response, next func()) {
		c := compose.Map{
			"header": func(req *f.Request, res *f.Response, next func()) {
				res.Send("Header string")
			},
			"body": func(req *f.Request, res *f.Response, next func()) {
				res.Render("body.html", "Title")
			},
			"footer": func(req *f.Request, res *f.Response, next func()) {
				res.End("Footer string")
			},
			"tail": func(req *f.Request, res *f.Response, next func()) {
				res.Write("Tail string")
			},
			"close": func(req *f.Request, res *f.Response, next func()) {
				res.WriteBytes([]byte("Close string"))
			},
		}
		data := c.Execute(req, res, next)
		res.Render("index.html", data)
	})
	app.Listen(3000)
}
Esempio n. 2
0
func main() {
	app := f.CreateApp()
	app.Engine(".html", mustache.Create())
	app.Get("/", func(req *f.Request, res *f.Response, next func()) {
		res.Render("index.html", map[string]string{"title": "Mustache"})
	})
	app.Listen(3000)
}
func TestResponseRender(t *testing.T) {

	var app *Application
	var req *Request
	var res *Response
	var buf *bytes.Buffer

	BeforeEach(func() {
		app, req, res, buf = CreateAppMock()
		app.Set("views", "./fixtures/views")
	})

	Describe("Render()", func() {

		It("should return HTML", func() {
			app.Engine(".html", mustache.Create())
			app.Get("/foo", func(req *Request, res *Response, next func()) {
				res.Render("index.html", map[string]string{"title": "Mu"})
			})
			req.Method = "GET"
			req.OriginalUrl = "/foo"
			app.Handle(req, res, 0)
			html := buf.String()
			AssertEqual(html, "<h1>Mu</h1>")
		})

		It("should return error", func() {
			app.Engine(".md", mustache.Create())
			app.Get("/foo", func(req *Request, res *Response, next func()) {
				res.Render("index.html", map[string]string{"title": "Mu"})
			})
			req.Method = "GET"
			req.OriginalUrl = "/foo"
			app.Handle(req, res, 0)
			html := buf.String()
			AssertEqual(html, "View engine for '.html' not found.")
		})
	})
}
Esempio n. 4
0
func init() {

	app := f.CreateApp()

	app.Use(responsetime.Create())
	app.Use(favicon.Create())
	app.Use(static.Create())

	app.Engine(".html", mustache.Create())

	app.Locals["title"] = "forgery2 - web application framework for golang"

	// API Reference Page.
	app.Get("/api.html", func(req *f.Request, res *f.Response, next func()) {
		res.Locals["title"] = "API Reference - forgery2"
		res.Render("index.html", map[string]string{
			"body":  markdown.Render("./en/api.md"),
			"class": "index",
		})
	})

	// Guide Page.
	app.Get("/guide.html", func(req *f.Request, res *f.Response, next func()) {
		res.Locals["title"] = "API Reference - forgery2"
		res.Render("index.html", map[string]string{
			"body":  markdown.Render("./en/guide.md"),
			"class": "index",
		})
	})

	// Default Page.
	app.Get("/", func(req *f.Request, res *f.Response, next func()) {
		res.Render("index.html", map[string]string{
			"body": markdown.Render("./en/home.md"),
		})
	})

	http.Handle("/", app)
}