ginraymond

package module
v0.0.0-...-740bde4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: MIT Imports: 6 Imported by: 0

README

ginraymond

Package ginraymond is a custom template renderer that can be used with the Gin web framework: https://github.com/gin-gonic/gin, it adds support for Handlebars style templates to your Gin application. It uses the Raymond template library for this: https://github.com/aymerick/raymond, which implements Handlebars style templates in pure Go.

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

Usage

To use ginraymond you need to set your router.HTMLRenderer instance to a custom renderer, this is done just after creating the Gin router.

You can either use ginraymond.Default() to create a new renderer with default options, this assumes that templates will be located in the "templates" directory, or you can use ginraymond.New() to specify a custom location with your own RenderOptions struct.

To render templates from a route call c.HTML() just as you would when rendering regular Gin templates from a route, except that instead of having to use gin.H{} for the template context, you can use any data type or struct supported by Raymond.

Basic Example

package main

import (
    "github.com/gin-gonic/gin"
    "gitlab.com/go-box/ginraymond"
)

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

    // Use gin.H or your own custom data types supported by Raymond.
    router.GET("/", func(c *gin.Context) {
        c.HTML(200, "hello.hbs", gin.H{"name": "world"})
    })

    router.Run(":8080")
}

Handlerbars Helpers Example

Register your handlebars helpers with Raymond the same way as in the Raymond docs:

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/aymerick/raymond"
    "gitlab.com/go-box/ginraymond"
)

// See Raymond docs on how to implement handlebars helpers.
func bold(options *raymond.Options) raymond.SafeString {
  return raymond.SafeString("<strong>" + options.Fn() + "</strong>")
}

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

    // Optionally register any custom Raymond handlerbars helpers.
    raymond.RegisterHelper("bold", bold)

    router.GET("/", func(c *gin.Context) {
        c.HTML(200, "hello.hbs", gin.H{"name": "world"})
    })

    router.Run(":8080")
}

RenderOptions

When calling ginraymond.New() instead of ginraymond.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.

Raymond does not implement it's own cache, so one is implemented in ginraymond.

GoDoc

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

Documentation

Overview

Package ginraymond is a custom template renderer that can be used with the Gin web framework: https://github.com/gin-gonic/gin, it adds support for Handlebars style templates to your Gin application. It uses the Raymond template library for this: https://github.com/aymerick/raymond, which implements Handlebars style templates in pure Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadTemplate

func LoadTemplate(filename string) (*raymond.Template, error)

LoadTemplate always loads the template from disk.

func MustLoadTemplate

func MustLoadTemplate(filename string) *raymond.Template

MustLoadTemplate loads the template from disk and panics on error.

Types

type RaymondRender

type RaymondRender struct {
	Options  *RenderOptions
	Cache    *TemplateCache
	Template *raymond.Template
	Context  interface{}
}

RaymondRender is a custom Gin template renderer using Raymond.

func Default

func Default() *RaymondRender

Default creates a RaymondRender instance with default options.

func New

func New(options *RenderOptions) *RaymondRender

New creates a new RaymondRender instance with custom Options.

func (RaymondRender) Instance

func (r RaymondRender) Instance(name string, data interface{}) render.Render

Instance should return a new RaymondRender struct for the current request and prepare the *raymond.Template that will be used for this response. Because of GIN's design, the Instance method cannot return an error type. This means that we have to panic on any errors which is not ideal in Go.

func (RaymondRender) Render

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

Render should write the content type, then render the template to the response.

func (RaymondRender) WriteContentType

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

WriteContentType writes header information about content RaymondRender outputs. This will now implement gin's render.Render interface.

type RenderOptions

type RenderOptions struct {
	TemplateDir string
	ContentType []string
}

RenderOptions is used to configure the renderer.

func DefaultOptions

func DefaultOptions() *RenderOptions

DefaultOptions constructs a RenderOptions struct with default settings.

type TemplateCache

type TemplateCache struct {
	TemplateDir string
	// contains filtered or unexported fields
}

TemplateCache manages a cache for raymond templates.

func (*TemplateCache) Get

func (c *TemplateCache) Get(filename string) (*raymond.Template, error)

Get either returns the template from the cache or load it from disk.

func (*TemplateCache) MustGet

func (c *TemplateCache) MustGet(filename string) *raymond.Template

MustGet either returns the template from the cache or load it from disk. On error this does a panic, if you don't want this then use the Get method.

Jump to

Keyboard shortcuts

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