### Install multitemplate Source: https://github.com/gin-contrib/multitemplate/blob/master/README.md Use the go get command to download and install the package. ```sh go get github.com/gin-contrib/multitemplate ``` -------------------------------- ### Implement simple template rendering Source: https://github.com/gin-contrib/multitemplate/blob/master/README.md Define a renderer and add templates using AddFromFiles to serve different HTML pages. ```go package main import ( "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func createMyRender() multitemplate.Renderer { r := multitemplate.NewRenderer() r.AddFromFiles("index", "templates/base.html", "templates/index.html") r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html") return r } func main() { router := gin.Default() router.HTMLRender = createMyRender() router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{ "title": "Html5 Template Engine", }) }) router.GET("/article", func(c *gin.Context) { c.HTML(200, "article", gin.H{ "title": "Html5 Article Engine", }) }) router.Run(":8080") } ``` -------------------------------- ### Implement dynamic layout patterns in Go Source: https://context7.com/gin-contrib/multitemplate/llms.txt Demonstrates a helper function to glob files and dynamically register templates by combining layouts and includes. ```go package main import ( "log" "path/filepath" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func loadTemplates(templatesDir string) multitemplate.Renderer { r := multitemplate.NewRenderer() layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") if err != nil { panic(err.Error()) } includes, err := filepath.Glob(templatesDir + "/includes/*.html") if err != nil { panic(err.Error()) } // Generate templates by combining each include with all layouts for _, include := range includes { layoutCopy := make([]string, len(layouts)) copy(layoutCopy, layouts) layoutCopy = append(layoutCopy, include) r.AddFromFiles(filepath.Base(include), layoutCopy...) } return r } func main() { router := gin.Default() router.HTMLRender = loadTemplates("./templates") router.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", gin.H{"title": "Welcome!"}) }) router.GET("/article", func(c *gin.Context) { c.HTML(200, "article.html", gin.H{"title": "Html5 Article Engine"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } // Directory structure: // templates/ // layouts/ // base.html // includes/ // index.html // article.html ``` -------------------------------- ### Import multitemplate Source: https://github.com/gin-contrib/multitemplate/blob/master/README.md Include the package in your Go source files. ```go import "github.com/gin-contrib/multitemplate" ``` -------------------------------- ### Create Agnostic Multi-Template Renderer Source: https://context7.com/gin-contrib/multitemplate/llms.txt Creates a new renderer that automatically switches between static mode (production) and dynamic mode (debug) based on Gin's current mode. In debug mode, templates are rebuilt on each request enabling hot reloading; in production mode, templates are compiled once for performance. Add templates using AddFromFiles, AddFromGlob, or AddFromFS. ```go package main import ( "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() // NewRenderer() returns DynamicRender in debug mode, Render in release mode r := multitemplate.NewRenderer() r.AddFromFiles("index", "templates/base.html", "templates/index.html") r.AddFromFiles("article", "templates/base.html", "templates/article.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"title": "Home Page"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Implement advanced template inheritance Source: https://github.com/gin-contrib/multitemplate/blob/master/README.md Dynamically load layouts and includes from directories to support template inheritance patterns. ```go package main import ( "path/filepath" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() router.HTMLRender = loadTemplates("./templates") router.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", gin.H{ "title": "Welcome!", }) }) router.GET("/article", func(c *gin.Context) { c.HTML(200, "article.html", gin.H{ "title": "Html5 Article Engine", }) }) router.Run(":8080") } func loadTemplates(templatesDir string) multitemplate.Renderer { r := multitemplate.NewRenderer() layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") if err != nil { panic(err.Error()) } includes, err := filepath.Glob(templatesDir + "/includes/*.html") if err != nil { panic(err.Error()) } // Generate our templates map from our layouts/ and includes/ directories for _, include := range includes { layoutCopy := make([]string, len(layouts)) copy(layoutCopy, layouts) files := append(layoutCopy, include) r.AddFromFiles(filepath.Base(include), files...) } return r } ``` -------------------------------- ### Load Multiple Template Layouts with Multitemplate Source: https://context7.com/gin-contrib/multitemplate/llms.txt Loads templates from specified directories using different base layouts for articles and admin sections. Ensure the template directory structure matches the glob patterns. ```go package main import ( "log" "path/filepath" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func loadTemplates(templatesDir string) multitemplate.Renderer { r := multitemplate.NewRenderer() // Load article templates with article base layout articleLayouts, _ := filepath.Glob(templatesDir + "/layouts/article-base.html") articles, _ := filepath.Glob(templatesDir + "/articles/*.html") for _, article := range articles { layoutCopy := make([]string, len(articleLayouts)) copy(layoutCopy, articleLayouts) layoutCopy = append(layoutCopy, article) r.AddFromFiles(filepath.Base(article), layoutCopy...) } // Load admin templates with admin base layout adminLayouts, _ := filepath.Glob(templatesDir + "/layouts/admin-base.html") admins, _ := filepath.Glob(templatesDir + "/admins/*.html") for _, admin := range admins { layoutCopy := make([]string, len(adminLayouts)) copy(layoutCopy, adminLayouts) layoutCopy = append(layoutCopy, admin) r.AddFromFiles(filepath.Base(admin), layoutCopy...) } return r } func main() { router := gin.Default() router.HTMLRender = loadTemplates("./templates") router.GET("/admin", func(c *gin.Context) { c.HTML(200, "admin.html", gin.H{"title": "Admin Panel"}) }) router.GET("/article", func(c *gin.Context) { c.HTML(200, "article.html", gin.H{"title": "Html5 Article Engine"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } // Directory structure: // templates/ // layouts/ // admin-base.html // article-base.html // admins/ // admin.html // articles/ // article.html ``` -------------------------------- ### Load Templates from File System with Custom Functions Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers templates from an fs.FS with custom template functions. Requires the templates to be accessible via the provided file system. ```go package main import ( "embed" "html/template" "log" "strings" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) //go:embed templates/* var templateFS embed.FS func main() { router := gin.Default() r := multitemplate.NewRenderer() // Define custom template functions funcMap := template.FuncMap{ "upper": strings.ToUpper, "lower": strings.ToLower, } // Load templates with custom functions from embedded FS r.AddFromFSFuncs("index", funcMap, templateFS, "templates/base.html", "templates/article.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"title": "Custom Functions"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create Templates from Multiple Strings with Functions Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers a template from multiple string fragments while supporting custom template functions. Ideal for template composition without external files. ```go package main import ( "html/template" "log" "strings" "time" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() funcMap := template.FuncMap{ "upper": strings.ToUpper, "formatDate": func(t time.Time) string { return t.Format("2006-01-02") }, } // Main template that includes a content block mainTemplate := `Welcome to {{ upper .name }} {{template "content"}}` contentTemplate := `{{define "content"}}Content Block{{end}}` r.AddFromStringsFuncs("index", funcMap, mainTemplate, contentTemplate) router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"name": "index"}) }) // Output: Welcome to INDEX Content Block if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Load Templates from Multiple Files Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers a named template composed of multiple HTML files. The first file typically serves as the base layout, with subsequent files providing content blocks. Files are parsed in order and can reference each other using Go's template inheritance syntax. ```go package main import ( "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() // Register "index" template with base layout + index content r.AddFromFiles("index", "templates/base.html", "templates/index.html") // Register "article" template with base layout + index + article content r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"title": "Html5 Template Engine"}) }) router.GET("/article", func(c *gin.Context) { c.HTML(200, "article", gin.H{"title": "Html5 Article Engine"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } // templates/base.html: // Title: {{ .title }} // index template: {{template "index.html"}} // templates/index.html: // Hi, this is index.html // templates/article.html: // {{define "article.html"}}Hi, this is article template{{end}} ``` -------------------------------- ### Register pre-compiled templates in Go Source: https://context7.com/gin-contrib/multitemplate/llms.txt Uses the Add method to register a manually parsed *template.Template object. ```go package main import ( "html/template" "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() // Create and configure template manually tmpl := template.Must(template.New("custom").Parse(` {{ .title }}

{{ .heading }}

{{ .content }}

`)) // Register pre-compiled template r.Add("custom", tmpl) router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "custom", gin.H{ "title": "Custom Template", "heading": "Welcome", "content": "This is a pre-compiled template", }) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Register templates with custom functions and delimiters in Go Source: https://context7.com/gin-contrib/multitemplate/llms.txt Uses AddFromFilesFuncsWithOptions to define custom template delimiters alongside a function map. ```go package main import ( "html/template" "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() funcMap := template.FuncMap{ "safeHTML": func(s string) template.HTML { return template.HTML(s) }, } options := multitemplate.TemplateOptions{ LeftDelimiter: "<%", RightDelimiter: "%>", } r.AddFromFilesFuncsWithOptions("erb-style", funcMap, options, "templates/base.html", "templates/content.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "erb-style", gin.H{"title": "ERB Style Delimiters"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Load Templates from Embedded File System Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers a named template from an `fs.FS` interface, enabling support for Go's embed package to bundle templates directly in your binary. ```go package main import ( "embed" "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) //go:embed templates/* var templateFS embed.FS func main() { router := gin.Default() r := multitemplate.NewRenderer() // Load templates from embedded file system r.AddFromFS("index", templateFS, "templates/base.html", "templates/article.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"title": "Embedded Template"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Register templates with custom functions in Go Source: https://context7.com/gin-contrib/multitemplate/llms.txt Uses AddFromFilesFuncs to associate a template name with specific files and a template.FuncMap. ```go package main import ( "html/template" "log" "strings" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() funcMap := template.FuncMap{ "upper": strings.ToUpper, "lower": strings.ToLower, "title": strings.Title, "contains": strings.Contains, } r.AddFromFilesFuncs("index", funcMap, "templates/welcome.html", "templates/content.html") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"name": "index"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create Templates with Custom Delimiters Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers templates using custom delimiters via TemplateOptions. Necessary when default template syntax conflicts with frontend frameworks. ```go package main import ( "html/template" "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() funcMap := template.FuncMap{} // Use custom delimiters to avoid conflicts with Vue.js {{ }} options := multitemplate.TemplateOptions{ LeftDelimiter: "[[", RightDelimiter: "]]", } // Template using custom [[ ]] delimiters templateStr := `
Hello [[ .name ]]!
` r.AddFromStringsFuncsWithOptions("vue-compatible", funcMap, options, templateStr) router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "vue-compatible", gin.H{"name": "World"}) }) // Output:
Hello World!
if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Load Templates Using Glob Pattern Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers a named template by matching files with a glob pattern. This is useful for loading all template files from a directory in one operation. ```go package main import ( "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() // Load all HTML files from tests/global/ directory r.AddFromGlob("index", "templates/global/*") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "index", gin.H{"title": "Test Multiple Template"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create Templates from String Literals Source: https://context7.com/gin-contrib/multitemplate/llms.txt Registers named templates directly from string content. Useful for simple or dynamically generated templates. ```go package main import ( "log" "github.com/gin-contrib/multitemplate" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() r := multitemplate.NewRenderer() // Create template directly from string r.AddFromString("greeting", "Welcome to {{ .name }} template") r.AddFromString("error", "

Error {{ .code }}

{{ .message }}

") router.HTMLRender = r router.GET("/", func(c *gin.Context) { c.HTML(200, "greeting", gin.H{"name": "index"}) }) // Output: Welcome to index template router.GET("/error", func(c *gin.Context) { c.HTML(500, "error", gin.H{"code": 500, "message": "Internal Server Error"}) }) if err := router.Run(":8080"); err != nil { log.Fatal(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.