Grender - Go package for easily rendering JSON/XML data and HTML templates

Grender GoDoc Build Status


Deprecation notice

This package could be more focused, so it was split up into two improved packages:


Grender is a package that provides functionality for easily rendering HTML templates and JSON or XML data to a HTTP response. It is based on github.com/unrolled/render with some subtle modifications when it comes to rendering HTML templates.

  • Templates inheritance: {{/* extends "master.tmpl" */}}
  • Glob configuration: templates/*.tmpl
  • Normal templates as partials: {{ template "footer" .}}

Usage

Grender can be used with pretty much any web framework providing you can access the http.ResponseWriter from your handler. The rendering functions simply wraps Go's existing functionality for marshaling and rendering data.

  • HTML: Uses the html/template package to render HTML templates.
  • JSON: Uses the encoding/json package to marshal data into a JSON-encoded response.
  • XML: Uses the encoding/xml package to marshal data into an XML-encoded response.
  • Text: Passes the incoming string straight through to the http.ResponseWriter.
// main.go
package main

import (
    "net/http"
    "github.com/dannyvankooten/grender"  
)

func main() {
    r := grender.New(grender.Options{
        Charset: "ISO-8859-1",
        TemplatesGlob: "examples/*.tmpl",
    })
    mux := http.NewServeMux()

    // This will set the Content-Type header to "application/json; charset=ISO-8859-1".
    mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
        r.JSON(w, http.StatusOK, map[string]string{"hello": "world"})
    })

    // This will set the Content-Type header to "text/html; charset=ISO-8859-1".
    mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) {
        r.HTML(w, http.StatusOK, "hello.tmpl", "world")
    })

    http.ListenAndServe("127.0.0.1:3000", mux)
}

Options

Grender comes with a variety of configuration options. The defaults are listed below.

r := grender.New(grender.Options{
    Debug: false,       // If true, templates will be recompiled before each render call
    TemplatesGlob: "",  // Glob to your template files
    PartialsGlob: "",   // Glob to your patials or global templates
    Funcs: nil,         // Your template FuncMap
    Charset: "UTF-8",   // Charset to use for Content-Type header values
})

Extending another template

First, define your parent template like this.

file: master.tmpl

<html>
  {{template "content" .}}
</html>

Then, in a separate template file use a template comment on the first line to indicate that you want to extend the other template file.

file: child.tmpl

{{/* extends "master.tmpl" */}}

{{define "content"}}Hello world!{{end}}

More examples

The grender_test.go file contains additional usage examples.

License

See LICENSE file.

Owner
Danny van Kooten
Independent developer, writing open-source code for a living.
Danny van Kooten
Similar Resources

Callable Ajax / http requests inside Golang templates

jaco Callable Ajax / http requests inside Golang templates Examples Examples #1 {{ define "content" }} div id="myTodos"/div script

Dec 5, 2021

Wrapper package for Go's template/html to allow for easy file-based template inheritance.

Extemplate Extemplate is a small wrapper package around html/template to allow for easy file-based template inheritance. File: templates/parent.tmpl

Dec 6, 2022

Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

goview Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Inst

Dec 25, 2022

Templating system for HTML and other text documents - go implementation

FAQ What is Kasia.go? Kasia.go is a Go implementation of the Kasia templating system. Kasia is primarily designed for HTML, but you can use it for any

Mar 15, 2022

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A language, command line tool and set of IDE extensions that makes it easier to write HTML user interfaces and websites using Go.

Dec 29, 2022

A template to build dynamic web apps quickly using Go, html/template and javascript

A template to build dynamic web apps quickly using Go, html/template and javascript

gomodest-template A modest template to build dynamic web apps in Go, HTML and sprinkles and spots of javascript. Why ? Build dynamic websites using th

Dec 29, 2022

Golang Echo and html template.

golang-website-example Golang Echo and html template. move GitHub repository for hello to golang-website-example Visual Studio Code Run and Debug: lau

Feb 4, 2022

A go template repository for easily bootstrapping new go applications

Template go Repository tl;dr This is a template go repository with actions already set up to create compiled releases What does this Template provide?

Nov 5, 2021

HTML template engine for Go

Ace - HTML template engine for Go Overview Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold. Exa

Jan 4, 2023
Go implementation for Soy templates (Google Closure templates)

soy Go implementation for Soy templates aka Google Closure Templates. See godoc for more details and usage examples. This project requires Go 1.12 or

Dec 1, 2022
Programatic document generation as a HTTP service. Render PDFs using LaTeX templates and JSON.
Programatic document generation as a HTTP service. Render PDFs using LaTeX templates and JSON.

LaTTe Generate PDFs using LaTeX templates and JSON. Try out the demo! Find LaTTe on Docker Hub Table of Contents About Obtaining LaTTe Running & Using

Dec 29, 2022
mold your templated to HTML/ TEXT/ PDF easily.
mold your templated to HTML/ TEXT/ PDF easily.

mold mold your templated to HTML/ TEXT/ PDF easily. install go get github.com/mayur-tolexo/mold Example 1 //Todo model type Todo struct { Title stri

Jun 7, 2019
Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering
 Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering

Go-Mojito is a super-modular library to bootstrap your next Go web project. It can be used for strict API-only purposes as well as server-side renderi

May 1, 2022
Fusozay Var Var: A CLI tool for quick text template rendering

fvv - Fusozay Var Var A CLI tool for quick text template rendering Fusozay Var Var means "have fun" It is a reference to something I see a lot Fusozay

Dec 11, 2021
Package damsel provides html outlining via css-selectors and common template functionality.

Damsel Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others. Library This package expects to exist

Oct 23, 2022
Useful template functions for Go templates.

Sprig: Template functions for Go templates The Go language comes with a built-in template language, but not very many template functions. Sprig is a l

Jan 4, 2023
Go templates invoked as functions

Package tmplfunc provides an extension of Go templates in which templates can be invoked as if they were functions. See the package documentation for

Dec 22, 2022
"to be defined" - a really simple way to create text templates with placeholders

tbd "to be defined" A really simple way to create text templates with placeholders. This tool is deliberately simple and trivial, no advanced features

Sep 27, 2022
Allows you to fill in variables in your custom project templates.

go-templater The best project templater go-templater lets you use any project template you want and replace the variables with values from the config.

Nov 6, 2021