Skip to content

Commit

Permalink
Add HTML template renderer (#29)
Browse files Browse the repository at this point in the history
* Add HTML template renderer

* Fix comment

* Template example, missed imports

* Update readme
  • Loading branch information
Ilya Kaznacheev authored and DronRathore committed Feb 17, 2019
1 parent 3a7ad4a commit 62f79f0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ func(req *express.Request, res *express.Response){
}
```

## HTML Template

Use [standard Golang http templates](https://golang.org/pkg/html/template/) to render response page.

For example, you have a template file `index.html` in `templates` directory:

```html
<h1>{{.Name}}</h1>
```

Fill the context and provide a path to the template file:

```go
func(req *express.Request, res *express.Response){
type TmplContext struct{
Name string
}
data := TmplContext{"Rob"}
res.Render("template.html", &data)
}
```

## Safe Cleanup on exit

Newer version of goexpress provides three new methods namely `express.ShutdownTimeout`, `express.BeforeShutdown` and `express.Shutdown`, these methods can be utilised to do cleanup before the server shuts down.
Expand Down
23 changes: 23 additions & 0 deletions examples/render/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/DronRathore/goexpress"
)

type Context struct {
Greeting string
Subject string
}

func main() {
express := goexpress.Express()
// return rendered template
express.Get("/", func(req goexpress.Request, res goexpress.Response) {
data := Context{
Greeting: "Hello",
Subject: "world",
}
res.Render("template.html", data)
})
express.Start("8080")
}
5 changes: 5 additions & 0 deletions examples/render/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>{{.Greeting}}, {{.Subject}}!</h1>
</body>
</html>
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Response interface {
SendFile(url string, noCache bool) bool
WriteBytes(bytes []byte) error
Write(content string) Response
Render(path string, data interface{})
}

// Header defines HTTP header interface
Expand Down
26 changes: 26 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package goexpress

import (
"bytes"
"bufio"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net"
Expand Down Expand Up @@ -307,3 +309,27 @@ func (res *response) Header() Header {
func (res *response) Cookie() Cookie {
return res.cookie
}

// Render returns rendered HTML template
func (res *response) Render(file string, data interface{}) {
tmpl, err := template.ParseFiles(file)
if err != nil {
log.Print("Template not found ", err)
res.header.SetStatus(500)
res.header.FlushHeaders()
res.End()
return
}

var tpl bytes.Buffer
err = tmpl.Execute(&tpl, data)
if err != nil {
log.Print("Template render failed ", err)
res.header.SetStatus(500)
res.header.FlushHeaders()
res.End()
return
}
res.WriteBytes(tpl.Bytes())
}

0 comments on commit 62f79f0

Please sign in to comment.