Back to

Package gintpl2x

Latest Go to latest

The latest major version is .

Published: Apr 1, 2019 | License: MIT | Module:


Package gintpl2x implements a gin frontend for tpl2x.



package main

import (

	mapper ""



func main() {

	// BufferPool size for rendered templates
	const bufferSize int = 64

	l := logrus.New()
	log := mapper.NewLogger(l)

	allFuncs := make(template.FuncMap)

	cfg := lookupfs.Config{
		Includes:   "inc",
		Layouts:    "layout",
		Pages:      "page",
		Ext:        ".tmpl",
		DefLayout:  "default",
		Index:      "index",
		HidePrefix: ".",
	// Here we attach an embedded filesystem
	fs := lookupfs.New(cfg).FileSystem(samplefs.FS())
	// Parse all of templates
	tfs, err := tpl2x.New(bufferSize).Funcs(allFuncs).LookupFS(fs).Parse()
	if err != nil {
	gintpl := gintpl2x.New(log, tfs)
	gintpl.RequestHandler = func(ctx *gin.Context, funcs template.FuncMap) gintpl2x.MetaData {
		setRequestFuncs(funcs, ctx)
		page := samplemeta.NewMeta(http.StatusOK, "text/html; charset=utf-8")
		return page

	r := gin.Default()
	gintpl.Route("", r)

	req, _ := http.NewRequest("GET", "/", nil)
	resp := httptest.NewRecorder()

	r.ServeHTTP(resp, req)



// setProtoFuncs appends function templates and not related to request functions to funcs
func setProtoFuncs(funcs template.FuncMap) {
	funcs["data"] = func() interface{} { return nil }
	funcs["request"] = func() interface{} { return nil }
	funcs["param"] = func(key string) string { return "" }
	funcs["HTML"] = func(s string) template.HTML {
		return template.HTML(s)

// setRequestFuncs appends funcs which return real data inside request processing
func setRequestFuncs(funcs template.FuncMap, ctx *gin.Context) {
	funcs["data"] = func() interface{} { return samplemeta.Data }
	funcs["request"] = func() interface{} { return ctx.Request }
	funcs["param"] = func(key string) string { return ctx.Param(key) }
text/html; charset=utf-8
  <title>index page</title>
  <h2>Test data</h2>
<h3>My TODO list</h3>
<li>Task 1
<li>Task 2
<li>Task 3
Host: <br />
URL: /<br />




const EngineKey = ""

EngineKey holds gin context key name for engine storage

type MetaData

type MetaData interface {
	ContentType() string // Returns content type
	Location() string    // Returns redirect url
	Status() int         // Response status

MetaData holds template metadata access methods

type Template

type Template struct {
	RequestHandler func(ctx *gin.Context, funcs template.FuncMap) MetaData
	// contains filtered or unexported fields

Template holds template engine attributes

func New

func New(log loggers.Contextual, fs TemplateService) *Template

New creates template object

func (Template) HTML

func (tmpl Template) HTML(ctx *gin.Context, uri string)

HTML renders page for given uri with context

func (*Template) Middleware

func (tmpl *Template) Middleware() gin.HandlerFunc

Middleware stores Engine in gin context

func (Template) Route

func (tmpl Template) Route(prefix string, r *gin.Engine)

Route registers template routes into gin

type TemplateService

type TemplateService interface {
	PageNames(hide bool) []string
	Render(w io.Writer, funcs template.FuncMap, data tpl2x.MetaData, content *bytes.Buffer) (err error)
	RenderContent(name string, funcs template.FuncMap, data tpl2x.MetaData) *bytes.Buffer

TemplateService allows to replace tpl2x functionality with the other package

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier