pongo2gin

package module
v5.1.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: MIT Imports: 5 Imported by: 0

README

Pongo2gin

Pongo2gin is an adapter that allows pongo2 (v5) to be used with the Gin web framework.

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

package main

import (
	"github.com/flosch/pongo2/v5"
	"github.com/gin-gonic/gin"
	"magnax.ca/pongo2gin/v5"
)

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")
}

Template Caching

Templates will be cached if the current Gin Mode is set to anything but "debug" (gin.DebugMode), 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 the 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://pkg.go.dev/magnax.ca/pongo2gin/v5

Documentation

Overview

Package pongo2gin is a template renderer that can provides a Gin-compatible template renderer using 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 Pongo2

type Pongo2 struct {
	Suffix string
	// contains filtered or unexported fields
}

Pongo2 is a custom Gin template renderer using Pongo2.

func Default

func Default() *Pongo2

Default creates a Pongo2 instance with default options.

func New

func New(set *pongo2.TemplateSet, suffix string) *Pongo2

New creates a new Pongo2 instance with custom Options.

func (Pongo2) Instance

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

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

type Render

type Render struct {
	// contains filtered or unexported fields
}

func (Render) Render

func (r Render) Render(w http.ResponseWriter) error

Render should render the template to the response.

func (Render) WriteContentType

func (r Render) WriteContentType(w http.ResponseWriter)

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

Jump to

Keyboard shortcuts

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