Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the method of increase Str #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<h1>hello, world</h1>")
})
m.Run()
}

Expand Down Expand Up @@ -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, "<h1>hello, world</h1>")
})
m.Run()
}

Expand Down
12 changes: 11 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"encoding/xml"
"fmt"
"html/template"
"io"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down