ginpongo2/README.md
2020-03-19 21:10:06 +01:00

1.3 KiB

pongo2 for gin

github.com/flosch/pongo2 master branch is used.

If you'd like to use pongo2.v3 see github.com/dalu/pongo2v3

        package main
        
        import (
         "github.com/gin-gonic/gin"
         "github.com/dalu/ginpongo2"
         "github.com/flosch/pongo2"
        )
        
        
        func main () {
        	r := gin.Default()

    		// templates is the basePath
    		// gin.IsDebugging() means if gin is in debug/dev mode then templates
    		// will always be reloaded from disk, otherwise from cache and never
    		// reloaded.
    		pr := ginpongo2.New("templates", gin.IsDebugging())
 
    		ginpongo2.Suffix = ".html.twig" // this is default
    		// when ever you render something reference it by prefix
    		// e.g. frontpage = frontpage.html.twig
    		
	    	r.HTMLRender = pr

    		r.GET("/", func(c *gin.Context) {
    			// only pongo2.Context or nil is accepted
	            ctx := make(pongo2.Context)
	            ctx["greeting"] = "hello world"
	            c.HTML(200, "frontpage", ctx)
	            })

    		r.Run(":8080")
        }

You can also supply TemplateLoaders. See pongo2.TemplateLoader documentation. It allows you to for example embed resources from a virtual filesystem.