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

Gin-Gonic with hero example #82

Open
borowiak opened this issue Mar 8, 2020 · 1 comment
Open

Gin-Gonic with hero example #82

borowiak opened this issue Mar 8, 2020 · 1 comment

Comments

@borowiak
Copy link

borowiak commented Mar 8, 2020

Hi everyone, I wrote code for Hero template system example and
I merged it with Gin framework, I didn't see any like this in documentation, I think this can be useful for someone else.
I based my code en 2 basic examples.
I don't sure where post this, so I opened a issues
please feel free to write any suggestion.

@gin-gonic

`package main

import (
"bytes"
"fmt"
"ginapp/template"
"io"
"net/http"

"github.com/gin-gonic/gin"
)

func main() {
router := gin.Default()
router.GET("/someDataFromReader", func(c *gin.Context) {
var buffer bytes.Buffer
var reader io.Reader
contentType := "text/html , utf8"

extraHeaders := map[string]string{
	"accept-ranges": "bytes",
}

var userList = []string{
	"Alice",
	"Bob",
	"Tom",
}

template.UserList(userList, &buffer)

nRead := int64(len(buffer.Bytes()))
reader = bytes.NewReader(buffer.Bytes())

fmt.Println(nRead)

c.DataFromReader(http.StatusOK, nRead, contentType, reader, extraHeaders)

})
router.Run(":8080")
}
`

@egodasa
Copy link

egodasa commented May 7, 2020

This works for me.

func (c CoreController) Pages(c *gin.Context) {
	var userList = []string{
		"Alice",
		"Bob",
		"Tom",
	}

	buffer := new(bytes.Buffer)
	views.UserList(userList, buffer)

	c.Data(c.HttpStatus, "text/html; charset=utf-8", buffer.Bytes())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants