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

Overview

goview

GoDoc Widget Travis Widget GoReportCard Widget

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

Contents

Install

go get github.com/foolin/goview

Features

  • Lightweight - use golang html/template syntax.
  • Easy - easy use for your web application.
  • Fast - Support configure cache template.
  • Include syntax - Support include file.
  • Master layout - Support configure master layout file.
  • Extension - Support configure template file extension.
  • Easy - Support configure templates directory.
  • Auto reload - Support dynamic reload template(disable cache mode).
  • Multiple Engine - Support multiple templates for frontend and backend.
  • No external dependencies - plain ol' Go html/template.
  • Gorice - Support gorice for package resources.
  • Gin/Iris/Echo/Chi - Support gin framework, Iris framework, echo framework, go-chi framework.

Docs

See https://www.godoc.org/github.com/foolin/goview

Supports

Usage

Overview

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
   

Use default instance:

    //write http.ResponseWriter
    //"index" -> index.html
    goview.Render(writer, http.StatusOK, "index", goview.M{})

Use new instance with config:

    gv := goview.New(goview.Config{
        Root:      "views",
        Extension: ".tpl",
        Master:    "layouts/master",
        Partials:  []string{"partials/ad"},
        Funcs: template.FuncMap{
            "sub": func(a, b int) int {
                return a - b
            },
            "copy": func() string {
                return time.Now().Format("2006")
            },
        },
        DisableCache: true,
	Delims:    Delims{Left: "{{", Right: "}}"},
    })
    
    //Set new instance
    goview.Use(gv)
    
    //write http.ResponseWriter
    goview.Render(writer, http.StatusOK, "index", goview.M{})

Use multiple instance with config:

    //============== Frontend ============== //
    gvFrontend := goview.New(goview.Config{
        Root:      "views/frontend",
        Extension: ".tpl",
        Master:    "layouts/master",
        Partials:  []string{"partials/ad"},
        Funcs: template.FuncMap{
            "sub": func(a, b int) int {
                return a - b
            },
            "copy": func() string {
                return time.Now().Format("2006")
            },
        },
        DisableCache: true,
	Delims:       Delims{Left: "{{", Right: "}}"},
    })
    
    //write http.ResponseWriter
    gvFrontend.Render(writer, http.StatusOK, "index", goview.M{})
    
    //============== Backend ============== //
    gvBackend := goview.New(goview.Config{
        Root:      "views/backend",
        Extension: ".tpl",
        Master:    "layouts/master",
        Partials:  []string{"partials/ad"},
        Funcs: template.FuncMap{
            "sub": func(a, b int) int {
                return a - b
            },
            "copy": func() string {
                return time.Now().Format("2006")
            },
        },
        DisableCache: true,
	Delims:       Delims{Left: "{{", Right: "}}"},
    })
    
    //write http.ResponseWriter
    gvBackend.Render(writer, http.StatusOK, "index", goview.M{})

Config

goview.Config{
    Root:      "views", //template root path
    Extension: ".tpl", //file extension
    Master:    "layouts/master", //master layout file
    Partials:  []string{"partials/head"}, //partial files
    Funcs: template.FuncMap{
        "sub": func(a, b int) int {
            return a - b
        },
        // more funcs
    },
    DisableCache: false, //if disable cache, auto reload template file for debug.
    Delims:       Delims{Left: "{{", Right: "}}"},
}

Include syntax

//template file
{{include "layouts/footer"}}

Render name:

Render name use index without .html extension, that will render with master layout.

  • "index" - Render with master layout.
  • "index.html" - Not render with master layout.
Notice: `.html` is default template extension, you can change with config

Render with master

//use name without extension `.html`
goview.Render(w, http.StatusOK, "index", goview.M{})

The w is instance of http.ResponseWriter

Render only file(not use master layout)

//use full name with extension `.html`
goview.Render(w, http.StatusOK, "page.html", goview.M{})

Custom template functions

We have two type of functions global functions, and temporary functions.

Global functions are set within the config.

goview.Config{
	Funcs: template.FuncMap{
		"reverse": e.Reverse,
	},
}
//template file
{{ reverse "route-name" }}

Temporary functions are set inside the handler.

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	err := goview.Render(w, http.StatusOK, "index", goview.M{
		"reverse": e.Reverse,
	})
	if err != nil {
		fmt.Fprintf(w, "Render index error: %v!", err)
	}
})
//template file
{{ call $.reverse "route-name" }}

Examples

See _examples/ for a variety of examples.

Basic example

package main

import (
	"fmt"
	"github.com/foolin/goview"
	"net/http"
)

func main() {

	//render index use `index` without `.html` extension, that will render with master layout.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "index", goview.M{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
		if err != nil {
			fmt.Fprintf(w, "Render index error: %v!", err)
		}

	})

	//render page use `page.tpl` with '.html' will only file template without master layout.
	http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "page.html", goview.M{"title": "Page file title!!"})
		if err != nil {
			fmt.Fprintf(w, "Render page.html error: %v!", err)
		}
	})

	fmt.Println("Listening and serving HTTP on :9090")
	http.ListenAndServe(":9090", nil)

}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Basic example

Gin example

go get github.com/foolin/goview/supports/ginview
package main

import (
	"github.com/foolin/goview/supports/ginview"
	"github.com/gin-gonic/gin"
	"net/http"
)

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

	//new template engine
	router.HTMLRender = ginview.Default()

	router.GET("/", func(ctx *gin.Context) {
		//render with master
		ctx.HTML(http.StatusOK, "index", gin.H{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	router.GET("/page", func(ctx *gin.Context) {
		//render only file, must full name with extension
		ctx.HTML(http.StatusOK, "page.html", gin.H{"title": "Page file title!!"})
	})

	router.Run(":9090")
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Gin example

Iris example

$ go get github.com/foolin/goview/supports/irisview
package main

import (
	"github.com/foolin/goview/supports/irisview"
	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.New()

	// Register the goview template engine.
	app.RegisterView(irisview.Default())

	app.Get("/", func(ctx iris.Context) {
		// Render with master.
		ctx.View("index", iris.Map{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	app.Get("/page", func(ctx iris.Context) {
		// Render only file, must full name with extension.
		ctx.View("page.html", iris.Map{"title": "Page file title!!"})
	})

	app.Listen(":9090")
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/iris" folder

Iris example

Iris multiple example

package main

import (
	"html/template"
	"time"

	"github.com/foolin/goview"
	"github.com/foolin/goview/supports/irisview"
	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.New()

	// Register a new template engine.
	app.RegisterView(irisview.New(goview.Config{
		Root:      "views/frontend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	}))

	app.Get("/", func(ctx iris.Context) {
		ctx.View("index", iris.Map{
			"title": "Frontend title!",
		})
	})

	//=========== Backend ===========//

	// Assign a new template middleware.
	mw := irisview.NewMiddleware(goview.Config{
		Root:      "views/backend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	backendGroup := app.Party("/admin", mw)

	backendGroup.Get("/", func(ctx iris.Context) {
		// Use the ctx.View as you used to. Zero changes to your codebase,
		// even if you use multiple templates.
		ctx.View("index", iris.Map{
			"title": "Backend title!",
		})
	})

	app.Listen(":9090")
}

Project structure:

|-- app/views/
    |-- fontend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        |-- partials/
     	   |--- ad.html
    |-- backend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        
See in "examples/iris-multiple" folder

Iris multiple example

Echo example

Echo <=v3 version:

go get github.com/foolin/goview/supports/echoview

Echo v4 version:

go get github.com/foolin/goview/supports/echoview-v4
package main

import (
	"github.com/foolin/goview/supports/echoview"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"net/http"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	//Set Renderer
	e.Renderer = echoview.Default()

	// Routes
	e.GET("/", func(c echo.Context) error {
		//render with master
		return c.Render(http.StatusOK, "index", echo.Map{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	e.GET("/page", func(c echo.Context) error {
		//render only file, must full name with extension
		return c.Render(http.StatusOK, "page.html", echo.Map{"title": "Page file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Echo example Echo v4 example

Go-chi example

package main

import (
	"fmt"
	"github.com/foolin/goview"
	"github.com/go-chi/chi"
	"net/http"
)

func main() {

	r := chi.NewRouter()

	//render index use `index` without `.html` extension, that will render with master layout.
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "index", goview.M{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
		if err != nil {
			fmt.Fprintf(w, "Render index error: %v!", err)
		}
	})

	//render page use `page.tpl` with '.html' will only file template without master layout.
	r.Get("/page", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "page.html", goview.M{"title": "Page file title!!"})
		if err != nil {
			fmt.Fprintf(w, "Render page.html error: %v!", err)
		}
	})

	fmt.Println("Listening and serving HTTP on :9090")
	http.ListenAndServe(":9090", r)

}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Chi example

Advance example

package main

import (
	"fmt"
	"github.com/foolin/goview"
	"html/template"
	"net/http"
	"time"
)

func main() {

	gv := goview.New(goview.Config{
		Root:      "views",
		Extension: ".tpl",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"sub": func(a, b int) int {
				return a - b
			},
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	//Set new instance
	goview.Use(gv)

	//render index use `index` without `.html` extension, that will render with master layout.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "index", goview.M{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
		if err != nil {
			fmt.Fprintf(w, "Render index error: %v!", err)
		}

	})

	//render page use `page.tpl` with '.html' will only file template without master layout.
	http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "page.tpl", goview.M{"title": "Page file title!!"})
		if err != nil {
			fmt.Fprintf(w, "Render page.html error: %v!", err)
		}
	})

	fmt.Println("Listening and serving HTTP on :9090")
	http.ListenAndServe(":9090", nil)
}

Project structure:

|-- app/views/
    |--- index.tpl          
    |--- page.tpl
    |-- layouts/
        |--- footer.tpl
        |--- head.tpl
        |--- master.tpl
    |-- partials/
        |--- ad.tpl
    

See in "examples/advance" folder

Advance example

Multiple example

package main

import (
	"html/template"
	"net/http"
	"time"

	"github.com/foolin/goview"
	"github.com/gin-gonic/gin"
)

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

	//new template engine
	router.HTMLRender = gintemplate.New(gintemplate.TemplateConfig{
		Root:      "views/fontend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	router.GET("/", func(ctx *gin.Context) {
		// `HTML()` is a helper func to deal with multiple TemplateEngine's.
		// It detects the suitable TemplateEngine for each path automatically.
		gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{
			"title": "Fontend title!",
		})
	})

	//=========== Backend ===========//

	//new middleware
	mw := gintemplate.NewMiddleware(gintemplate.TemplateConfig{
		Root:      "views/backend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	// You should use helper func `Middleware()` to set the supplied
	// TemplateEngine and make `HTML()` work validly.
	backendGroup := router.Group("/admin", mw)

	backendGroup.GET("/", func(ctx *gin.Context) {
		// With the middleware, `HTML()` can detect the valid TemplateEngine.
		gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{
			"title": "Backend title!",
		})
	})

	router.Run(":9090")
}

Project structure:

|-- app/views/
    |-- fontend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        |-- partials/
     	   |--- ad.html
    |-- backend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        
See in "examples/multiple" folder

Multiple example

go.rice example

go get github.com/foolin/goview/supports/gorice
package main

import (
	"fmt"
	"github.com/GeertJohan/go.rice"
	"github.com/foolin/goview"
	"github.com/foolin/goview/supports/gorice"
	"net/http"
)

func main() {

	//static
	staticBox := rice.MustFindBox("static")
	staticFileServer := http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox()))
	http.Handle("/static/", staticFileServer)

	//new view engine
	gv := gorice.New(rice.MustFindBox("views"))
	//set engine for default instance
	goview.Use(gv)

	//render index use `index` without `.html` extension, that will render with master layout.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "index", goview.M{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
		if err != nil {
			fmt.Fprintf(w, "Render index error: %v!", err)
		}

	})

	//render page use `page.tpl` with '.html' will only file template without master layout.
	http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
		err := goview.Render(w, http.StatusOK, "page.html", goview.M{"title": "Page file title!!"})
		if err != nil {
			fmt.Fprintf(w, "Render page.html error: %v!", err)
		}
	})

	fmt.Println("Listening and serving HTTP on :9090")
	http.ListenAndServe(":9090", nil)
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
|-- app/static/  
    |-- css/
        |--- bootstrap.css   	
    |-- img/
        |--- gopher.png

See in "examples/gorice" folder

gorice example

More examples

See _examples/ for a variety of examples.

Todo

[ ] Add Partials support directory or glob [ ] Add functions support.

Issues
  • Auto complete Extensions

    Auto complete Extensions

    The file extension should not be added if it's already provided in the include instruction:

    Example:
    I use the default configuration of goview, (so the Extensions is ".html")

    error.html:

    {{define "head"}}
        {{include "style.css"}}
    {{end}}
    
    {{define "content"}}
        <h1>Error</h1>
    {{end}}
    

    style.css:

    body {
    background-color: red;
    }
    

    The error I get:

    ViewEngine execute template error: template: error:2:6: executing "head" at <include "style.css">: error calling include: ViewEngine render read name:style.css, path:<gopath>/github.com/<username>/<projectname>/service.web.oauth/views/style.css.html, error: open <gopath>/github.com/<username>/<projetctname>/service.web.oauth/views/style.css.html: no such file or directory
    
    opened by ArthurKnoep 6
  • Question about how to call a function

    Question about how to call a function

    What is the diference about calling a function with/without {{ call $.funname params }} ?

    I've been trying to add the reverse URL function to the function and it only works without call $. but I'm not sure what it's right or wrong since this is not documented

    My render

    	//Set Renderer
    	e.Renderer = echoview.New(goview.Config{
    		Root:      "app/templates",
    		Extension: ".html",
    		Master:    "layouts/master",
    		Partials:  []string{},
    		Funcs: template.FuncMap{
    			"reverse": e.Reverse,
    		},
    		DisableCache: false,
    	})
    

    This works

    {{ reverse "board_create" }}
    

    This throws an error

    {{ call $.reverse "board_create" }}
    

    Error: "ViewEngine execute template error: template: layouts/master:15:6: executing "layouts/master" at \u003cinclude "layouts/header"\u003e: error calling include: ViewEngine execute template error: template: layouts/header:15:23: executing "layouts/header" at \u003ccall $.reverse "board_create"\u003e: error calling call: call of nil"

    opened by wsantos 5
  • Could you tell me the difference between include tag and template tag?

    Could you tell me the difference between include tag and template tag?

    Could you tell me the difference?

    {{template "content" .}} {{include "content"}}

    opened by saito-kosuke 5
  • Context pre-processors

    Context pre-processors

    Hi I need to extract data from context and expose it to the template, mainly sure info so I can show that the user is logged in on the page header, do we have a way to do it ? do you think that maybe we can have the same functionality as Config.Funcs but they receive context? here it's my test middleware and the data I need available on all templates.

    // UserAuth -
    func (m *Middleware) UserAuth(next echo.HandlerFunc) echo.HandlerFunc {
    	return func(c echo.Context) error {
    		sess, _ := session.Get("session", c)
    		userId, _ := sess.Values["user_id"].(int)
    
    		user, _ := models.FindUser(c.Request().Context(), m.DB, userId)
    
    		c.Set("user", user)
    
    		return next(c)
    	}
    }
    

    And for echoview I think that this needs to go here, echoview.go

    // Render render template for echo interface
    func (e *ViewEngine) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
    
        // call all context processors and transform data
    
    	return e.RenderWriter(w, name, data)
    }
    

    Let me know if that is a good approach, but I'm not sure how to fit this on other frameworks.

    opened by wsantos 4
  • How can i use go.rice in gin via goview, like git-template

    How can i use go.rice in gin via goview, like git-template

    gorice and gin is work fine in foolin/gin-template

    router.HTMLRender = gorice.New(rice.MustFindBox("views"))

    but now, I will use go.rice via goview, built app throw error no such file or directory

    panic recovered:
    ViewEngine render read name:base/master, path:/tmp/templates/views/base/master.tmpl, error: open /tmp/templates/views/base/master.tmpl: no such file or directory
    

    I think go.rice is not working.

    my code:

    r := gin.Default()
    
    staticBox := rice.MustFindBox("templates/static")
    viewBox := rice.MustFindBox("templates/views")
    r.StaticFS("/static", staticBox.HTTPBox())
    
    gv := gorice.NewWithConfig(viewBox, goview.Config{
      Root:      "templates/views",
      Extension: ".tmpl",
      Master:    "base/master",
    })
    goview.Use(gv)
    
    r.HTMLRender = ginview.New(goview.Config{
      Root:      "templates/views",
      Extension: ".tmpl",
      Master:    "base/master",
    })
    
    r.GET("/", func(c *gin.Context) {
    	ginview.HTML(c, http.StatusOK, "index", gin.H{
    		"title":                  "Gin",
    		"ginBladeManagerVersion": "v1",
    		"goVersion":              runtime.Version(),
    	})
    })
    

    How do I make gorice work in gin via goview?

    opened by palytoxin 4
  • How to parse html template in to echo render?

    How to parse html template in to echo render?

    Is there any option to parse some string like "{{.Title}}"?

    Example return c.Render(http.StatusOK,index), echo.Map{"Render": "{{.Title}}"})

    opened by aigoya 3
  • Compile templates into server executable

    Compile templates into server executable

    Not sure if this is possible, but I'm trying to compile the templates/static files into the binary so that when I run the executable elsewhere, I don't also have to figure out how to ship assets - that is to say, it's all included within the compiled go binary.

    I've done this before with static assets utilizing go-bindata, but not sure if there is a way to do it for goview.

    In my case, I'm specifically utilizing goview with gin (via ginview) but hopefully that doesn't impact the answer.

    Great work btw, this package looks awesome!

    opened by josegonzalez 3
  • Additional funcs for templates

    Additional funcs for templates

    Hello.

    Please help with advice. I don’t understand how I can add a new function after initializing the config. For example, if we take the example "/multiple/main.go" we have two configs like gvFront and gvBackend initialized before route. How I can add a new function T into handler like:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    		err := gvFront.Render(w, http.StatusOK, "index", goview.M{
    			"title": "Frontend title!",
                            "T": func(id string) string { return "AAAAAAAAAA!" },
    		})
    		if err != nil {
    			fmt.Fprintf(w, "Render index error: %v!", err)
    		}
    	})
    

    If we run this code, we will see an error:

    Render index error: ViewEngine render parser name:index,
    error: template: index:2: function "T" not defined!
    

    Adding a function in the process is a prerequisite, I do not need this function at the initialization stage of the config.

    opened by johndark 2
  • Had to abandon this tool

    Had to abandon this tool

    This engine does not currently work(at least echo view) and had to be abandoned for a project I was working on. You are not able to pass data from the handlers to the templates, instead it crashes the Echo app.

    I'm not sure if this is true of the other versions, but Echoview is unusable.

    opened by dy-fi 2
  • How i can print html comment like knockout-if

    How i can print html comment like knockout-if

    There are some js library need using comment like knockout, how i can achieve this using goview?

    <ul>
        <li>This item always appears</li>
        <!-- ko if: someExpressionGoesHere -->
            <li>I want to make this item present/absent dynamically</li>
        <!-- /ko -->
    </ul>
    
    opened by raditzlawliet 2
  • extends syntax

    extends syntax

    How hard would it be to add a extends function so we can have "multiple" masters, I need to have some pages without footer and header and right now it's set on the master layout, without it, I'll need to set footer and master on most of my pages just to have 1/2 pages without footer and master, thanks in advance.

    opened by wsantos 0
  • Embeded Static file support on golang 1.16 standard Example

    Embeded Static file support on golang 1.16 standard Example

    I see issue #9 that you have support on go.rice https://github.com/foolin/goview/issues/9 do you have plan to support go embeded or do you have example to do it? https://golang.org/pkg/embed/

    opened by golfapipol 1
  • docs: Example for if/else

    docs: Example for if/else

    Synopsis

    There are no examples for if/else (should this parser even support it.)

    opened by AlbinoGeek 1
  • docs: Example for range/each

    docs: Example for range/each

    Synopsis

    There are no examples for range/each.

    {{range $e := .providers}}
    <li><a href="/login/w/{{$e}}" class="button">
      <i class="fa fa-{{$e}}"></i> {{ucfirst $e}}
    </a></li>
    {{end}}
    

    https://golang.org/pkg/text/template/

    opened by AlbinoGeek 0
  • gin how to embed the templates in binary?

    gin how to embed the templates in binary?

    go 1.16 embed

    opened by iwalkerr 1
  • go get error

    go get error

    OS: Windows 10 Pro 64 bit go version: go1.15.4 windows/amd64

    Apologies if this is a GitHub issue vs. something specific to this repository. I'm still learning how the go module system interacts with git.

    I have a simple go Echo app running that uses the default Echo templating engine. The app imports a number of modules successfully, e.g.-

    import (
    	"log"
    	"net/http"
    	"os"
    	"os/signal"
    	"syscall"
    
    	"github.com/labstack/echo/v4"
    	"github.com/labstack/echo/v4/middleware"
    
    	"gitlab.com/colin_e_nhsd/eprom.git/backend"
    )
    

    this works fine.

    However when I tried to add goview-v4 I was unable to access the repo. If I run-

    go get github.com/foolin/goview-v4 as a first step in either a Powershell window (I'm on Windows) or the VSCode terminal I get two popup GitHub password prompts one after the other, then the download aborts after a lengthy timeout.

    UPDATE I think this error is a combination of a documentation issue on page- https://github.com/foolin/goview/tree/master/supports/echoview-v4 plus the fact that Github gives confusing errors about authentication failures when what it means is "repo or page not found".

    On the supports/echoview-v4 doc page the first few lines say-

    Install

    go get -u github.com/foolin/goview-v4
    go get -u github.com/foolin/goview/supports/echoview-v4
    

    Example

    package main
    
    import (
    	"github.com/foolin/goview/supports/echoview"
    	"github.com/labstack/echo"
    	"github.com/labstack/echo/middleware"
    	"net/http"
    )
    

    the line go get -u github.com/foolin/goview-v4 fails because there is no such module or package. The following line go get -u github.com/foolin/goview/supports/echoview-v4 succeeds, but I assume that means thre will be unresolved dependencies.

    Meanwhile, the imports don't seem to be consistent either with the module dependencies above (no -v4 suffix) nor the correct imports for Echo v4 itself, which is imported as-

    	"github.com/labstack/echo/v4"
    	"github.com/labstack/echo/v4/middleware"
    

    Any chance of updating the documentation here? I'm not entirely clear if this package is module-aware, which is significant now Echo v4 is set up as modules.

    opened by colin-e-nhsd 1
  • How Can I Manual clear page cache?

    How Can I Manual clear page cache?

    1、my page was dynanic changed , I need to clear tplMap cache. Is there a good way for me to solve this problem?

    opened by wzhsh90 3
  • Issue when using on Gin Framework

    Issue when using on Gin Framework

    I'm trying to use goview on Gin Framework.

    I did following from one of the example:

    r.HTMLRender = ginview.New(goview.Config{
    		Root:      "./fe/views",
    		Extension: ".html",
    		Master:    "layouts/master",
    		Partials:  []string{},
    		Funcs: template.FuncMap{
    			"copy": func() string {
    				return time.Now().Format(strconv.Itoa(time.Now().Year()))
    			},
    		},
    		DisableCache: false,
    	})
    

    master.html:

    <!-- /views/admin/master.html -->
    <!doctype html>
    
    <html>
    <head>
        <title>{{.title}}</title>
        {{template "head" .}}
    </head>
    
    <body>
    {{template "content" .}}
    <hr>
    {{include "layouts/footer"}}
    </body>
    </html>
    

    But when trying to access the page, I stumbled with following error:

    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
    
    
    ViewEngine execute template error: html/template:layouts/master:11:11: no such template "content"
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:842 (0x155efa2)
            (*Context).Render: panic(err)
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:851 (0x155f048)
            (*Context).HTML: c.Render(code, instance)
    /Users/sujit/Sites/labs/gin-rest/be/app/app.go:43 (0x157c533)
            Serve.func2: ctx.HTML(http.StatusOK, "index", gin.H{
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
            (*Context).Next: c.handlers[c.index](c)
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0x156eb2f)
            RecoveryWithWriter.func1: c.Next()
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
            (*Context).Next: c.handlers[c.index](c)
    /Users/sujit/Sites/labs/gin-rest/be/app/Http/Middleware/request_id.go:23 (0x1572938)
            RequestId.func1: c.Next()
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
            (*Context).Next: c.handlers[c.index](c)
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0x156eb2f)
            RecoveryWithWriter.func1: c.Next()
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
            (*Context).Next: c.handlers[c.index](c)
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0x156dc60)
            LoggerWithConfig.func1: c.Next()
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
            (*Context).Next: c.handlers[c.index](c)
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:409 (0x1565205)
            (*Engine).handleHTTPRequest: c.Next()
    /Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:367 (0x156491c)
            (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    /Users/sujit/go/go1.14.2/src/net/http/server.go:2807 (0x12d4072)
            serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
    /Users/sujit/go/go1.14.2/src/net/http/server.go:1895 (0x12cf9eb)
            (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
    /Users/sujit/go/go1.14.2/src/runtime/asm_amd64.s:1373 (0x1064870)
            goexit: BYTE    $0x90   // NOP
    

    Shouldn't it work even though template "content" . exists

    opened by sujit-baniya 7
  • How to use Multitemplate for gin?

    How to use Multitemplate for gin?

    I see in your example using different template like 'gvFrontend' and 'gvBackend', But in gin, we can only pass one ViewEngine. Is there any example for gin?

    opened by raditzlawliet 2
  • Support for `PartialsDir` or `PartialsGlob`

    Support for `PartialsDir` or `PartialsGlob`

    Golang's native html supports template.ParseGlob, which I find quite useful. And for example, another template engine, extemplate, supports ParseDir.

    Currently, goview requires all partials to be listed in goview.Config. Would it be possible to add support to include partials recursively from specific base directory? Or, include partials matching specific glob?

    opened by kravemir 7
Releases(v0.3.0)
Owner
foolin
foolin
Handlebars for golang

raymond Handlebars for golang with the same features as handlebars.js 3.0. The full API documentation is available here: http://godoc.org/github.com/a

Aymerick 441 Sep 13, 2021
Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade

amber Notice While Amber is perfectly fine and stable to use, I've been working on a direct Pug.js port for Go. It is somewhat hacky at the moment but

Ekin Koc 879 Aug 23, 2021
Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

quicktemplate A fast, powerful, yet easy to use template engine for Go. Inspired by the Mako templates philosophy. Features Extremely fast. Templates

Aliaksandr Valialkin 2.2k Sep 22, 2021
A general purpose golang CLI template for Github and Gitlab

golang-cli-template A general purpose project template for golang CLI applications This template serves as a starting point for golang commandline app

null 11 Aug 12, 2021
Razor view engine for go

gorazor gorazor is the Go port of the razor view engine originated from asp.net in 2011. In summary, gorazor is: Extremely Fast. Templates are convert

null 787 Sep 13, 2021
A handy, fast and powerful go template engine.

Hero Hero is a handy, fast and powerful go template engine, which pre-compiles the html templates to go code. It has been used in production environme

Lime 1.5k Sep 20, 2021
comparing the performance of different template engines

goTemplateBenchmark comparing the performance of different template engines full featured template engines Ace Amber Go Handlebars removed - Kasia Mus

null 189 Aug 31, 2021
A demonstrative template for creating reliable Terraform modules

This repository provides a template for creating new Terraform modules. It's intended to demonstrate how one might go about standardizing their modules and subjecting them to integration tests in CI.

Patrick Delaney 3 Sep 1, 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

Keiji Yoshida 805 Sep 22, 2021
A sweet velvety templating package

Velvet Velvet is a templating package for Go. It bears a striking resemblance to "handlebars" based templates, there are a few small changes/tweaks, t

Buffalo - The Go Web Eco-System 73 Sep 17, 2021
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

Michał Derkacz 72 May 4, 2021
The world’s most powerful template engine and Go embeddable interpreter.

The world’s most powerful template engine and Go embeddable interpreter

Open2b 17 Sep 15, 2021
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

null 2.6k Sep 24, 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

Daniel Skinner 24 Sep 12, 2020
pongo2 is a Django-syntax like templating-language

Django-syntax like template-engine for Go

Florian Schlachter 2.1k Sep 25, 2021
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.

Adrian Hesketh 80 Sep 18, 2021
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

Raphael Reyna 173 Sep 18, 2021
The powerful template system that Go needs

Plush Plush is the templating system that Go both needs and deserves. Powerful, flexible, and extendable, Plush is there to make writing your template

Buffalo - The Go Web Eco-System 657 Sep 22, 2021
The mustache template language in Go

Overview mustache.go is an implementation of the mustache template language in Go. It is better suited for website templates than Go's native pkg/temp

Michael Hoisie 1k Sep 21, 2021