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

Added new methods to Render interface #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
package render

import (
"bufio"
"bytes"
"encoding/json"
"encoding/xml"
Expand All @@ -60,6 +61,8 @@ const (
ContentText = "text/plain"
ContentJSON = "application/json"
ContentHTML = "text/html"
ContentCSS = "text/css"
ContentJS = "text/javascript"
ContentXHTML = "application/xhtml+xml"
ContentXML = "text/xml"
defaultCharset = "UTF-8"
Expand All @@ -85,6 +88,8 @@ type Render interface {
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.
HTML(status int, name string, v interface{}, htmlOpt ...HTMLOptions)
// CSSJS renders a css or js template specified by the name and writes the result and given status to the http.ResponseWriter.
CSSJS(status int, directory string)
// XML writes the given status and XML serialized version of the given value to the http.ResponseWriter.
XML(status int, v interface{})
// Data writes the raw byte array to the http.ResponseWriter.
Expand All @@ -101,6 +106,10 @@ type Render interface {
Template() *template.Template
// Header exposes the header struct from http.ResponseWriter.
Header() http.Header
// GetRequest retrieve http request for using the associated information
GetRequest() *http.Request
// GetResponseWriter retrieve http response for using the associated information
GetResponseWriter() *renderer
}

// Delims represents a set of Left and Right delimiters for HTML template rendering
Expand Down Expand Up @@ -296,6 +305,35 @@ func (r *renderer) HTML(status int, name string, binding interface{}, htmlOpt ..
bufpool.Put(buf)
}

func (r *renderer) CSSJS(status int, directory string) {
arrayURL := strings.Split(r.req.URL.Path, "/")
path := arrayURL[len(arrayURL)-1]

var content string
if strings.HasSuffix(path, ".css") {
content = ContentCSS
} else if strings.HasSuffix(path, ".js") {
content = ContentJS
} else {
content = ContentText
}

if directory > "" {
path = directory + "/" + path
}

f, err := os.Open(path)

if err == nil {
defer f.Close()
r.Header().Add(ContentType, content)
br := bufio.NewReader(f)
br.WriteTo(r)
} else {
r.WriteHeader(http.StatusNotFound)
}
}

func (r *renderer) XML(status int, v interface{}) {
var result []byte
var err error
Expand Down Expand Up @@ -384,3 +422,11 @@ func (r *renderer) prepareHTMLOptions(htmlOpt []HTMLOptions) HTMLOptions {
Layout: r.opt.Layout,
}
}

func (r *renderer) GetRequest() *http.Request {
return r.req
}

func (r *renderer) GetResponseWriter() *renderer {
return r
}