Files
kalorienmanager/pkg/util/render.go
T
2019-06-18 18:33:38 +02:00

40 lines
1.1 KiB
Go

package util
import (
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/auth"
"net/http"
"github.com/gin-gonic/gin"
)
const (
// SaveAction defines label of button.
SaveAction = "Save"
// AddAction defines label of button.
AddAction = "Add"
// DeleteAction defines label of button.
DeleteAction = "Delete"
)
// Render one of HTML, JSON or CSV based on the 'Accept' header of the request
// If the header doesn't specify this, HTML is rendered, provided that
// the template name is present
func Render(c *gin.Context, data gin.H, templateName string) {
auth.SetSessionData(c, data)
switch c.Request.Header.Get("Accept") {
case "application/json":
c.JSON(http.StatusOK, data["payload"])
case "application/xml":
c.XML(http.StatusOK, data["payload"])
default:
c.HTML(http.StatusOK, templateName, data)
}
}
// ShowIndexPage render the index page of Kontor web application.
func ShowIndexPage(c *gin.Context) {
// Call the render function with the name of the template to render
//log.Printf("Context: %v", c)
Render(c, gin.H{"title": "Kalorienmanager", "payload": nil}, "index.html")
}