From bf166bfe029b5e3d194a1528988dd15911099db5 Mon Sep 17 00:00:00 2001 From: oumaharuki <1781808576@qq.com> Date: Wed, 25 Jul 2018 09:54:33 +0800 Subject: [PATCH] the method of increase Str --- README.md | 10 ++++++++-- render.go | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d7be3a0..d4bd612 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,10 @@ func main() { m.Get("/text", func(r render.Render) { r.Text(200, "hello, world") }) - + //This will set the Content-Type header to "text/html; charset=UTF-8" + m.Get("/string", func(r render.Render) { + r.Str(200, "

hello, world

") + }) m.Run() } @@ -201,7 +204,10 @@ func main() { m.Get("/text", func(r render.Render) { r.Text(200, "hello, world") }) - + //This will set the Content-Type header to "text/html; charset=UTF-8" + m.Get("/string", func(r render.Render) { + r.Str(200, "

hello, world

") + }) m.Run() } diff --git a/render.go b/render.go index 0cacb97..990c3cc 100644 --- a/render.go +++ b/render.go @@ -41,7 +41,7 @@ import ( "encoding/xml" "fmt" "html/template" - "io" + "io" "io/ioutil" "net/http" "os" @@ -81,6 +81,8 @@ var helperFuncs = template.FuncMap{ // Render is a service that can be injected into a Martini handler. Render provides functions for easily writing JSON and // HTML templates out to a http Response. type Render interface { + //String writes the given status and plain text to the http.ResponseWriter. + Str(status int, v string) // JSON writes the given status and JSON serialized version of the given value to the http.ResponseWriter. JSON(status int, v interface{}) // HTML renders a html template specified by the name and writes the result and given status to the http.ResponseWriter. @@ -253,6 +255,14 @@ type renderer struct { compiledCharset string } +func (r *renderer) Str(status int, v string) { + if r.Header().Get(ContentType) == "" { + //r.Header().Set(ContentType, ContentText+r.compiledCharset) + r.Header().Set(ContentType, "text/html") + } + r.WriteHeader(status) + r.Write([]byte(v)) +} func (r *renderer) JSON(status int, v interface{}) { var result []byte var err error