pongo2gin

package module
v0.0.0-...-2be2c53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2019 License: MIT Imports: 5 Imported by: 1

README

Pongo2gin

Forked from: https://gitlab.com/go-box/pongo2gin

Package pongo2gin is a template renderer that can be used with the Gin web framework https://github.com/gin-gonic/gin it uses the Pongo2 template library https://github.com/flosch/pongo2

This simple binding library is based on a similar library built for using Handlebars templates with Gin: https://gitlab.com/go-box/ginraymond.

Requirements

Requires Gin 1.2 or higher and Pongo2.

Usage

To use pongo2gin you need to set your router.HTMLRenderer to a new renderer instance, this is done after creating the Gin router when the Gin application starts up. You can use pongo2gin.Default() to create a new renderer with default options, this assumes templates will be located in the "templates" directory, or you can use pongo2.New() to specify a custom location.

To render templates from a route, call c.HTML just as you would with regular Gin templates, the only difference is that you pass template data as a pongo2.Context instead of gin.H type.

Basic Example

import (
    "github.com/gin-gonic/gin"
    "github.com/flosch/pongo2"
    "gitlab.com/go-box/pongo2gin"
)

func main() {
    router := gin.Default()

    // Use pongo2gin.Default() for default options or pongo2gin.New()
    // if you need to use custom RenderOptions.
    router.HTMLRender = pongo2gin.Default()

    router.GET("/", func(c *gin.Context) {
        // Use pongo2.Context instead of gin.H
        c.HTML(200, "hello.html", pongo2.Context{"name": "world"})
    })

    router.Run(":8080")
}

RenderOptions

When calling pongo2gin.New() instead of pongo2gin.Default() you can use these custom RenderOptions:

type RenderOptions struct {
    TemplateDir string  // location of the template directory
    ContentType string  // Content-Type header used when calling c.HTML()
}

Template Caching

Templates will be cached if the current Gin Mode is set to anything but "debug", this means the first time a template is used it will still load from disk, but after that the cached template will be used from memory instead.

If he Gin Mode is set to "debug" then templates will be loaded from disk on each request.

Caching is implemented by the Pongo2 library itself.

GoDoc

https://godoc.org/gitlab.com/go-box/pongo2gin

Documentation

Overview

Package pongo2gin is a template renderer that can be used with the Gin web framework https://github.com/gin-gonic/gin it uses the Pongo2 template library https://github.com/flosch/pongo2

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pongo2Render

type Pongo2Render struct {
	Options  *RenderOptions
	Template *pongo2.Template
	Context  pongo2.Context
}

Pongo2Render is a custom Gin template renderer using Pongo2.

func Default

func Default() *Pongo2Render

Default creates a Pongo2Render instance with default options.

func New

func New(options RenderOptions) *Pongo2Render

New creates a new Pongo2Render instance with custom Options.

func (Pongo2Render) Instance

func (p Pongo2Render) Instance(name string, data interface{}) render.Render

Instance should return a new Pongo2Render struct per request and prepare the template by either loading it from disk or using pongo2's cache.

func (Pongo2Render) Render

func (p Pongo2Render) Render(w http.ResponseWriter) error

Render should render the template to the response.

func (Pongo2Render) WriteContentType

func (p Pongo2Render) WriteContentType(w http.ResponseWriter)

WriteContentType should add the Content-Type header to the response when not set yet.

type RenderOptions

type RenderOptions struct {
	TemplateDir string
	ContentType string
}

RenderOptions is used to configure the renderer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL