• With issues:
  • Use the search tool before opening a new issue.
  • Please provide source code and commit sha if you found a bug.
  • Review existing issues and provide feedback or react to them.

Description

How to reproduce

package main

import (
    "log"
    "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",
        })
    })

    if err := router.Run(":8080"); err != nil {
        log.Fatal(err)
    }
}

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
}

base.html

{{define "base"}}

index template: {{block "content" .}}

{{end}}
{{end}}

index.html

{{template "base ."}}

{{define "content"}}
Hi, this is index.html
{{end}}

article.html

{{template "base ."}}

{{define "content"}}
Hi, this is article.html
{{end}}

Expectations

$ curl http://localhost:8080/article

Actual result

![image](https://user-images.githubusercontent.com/46656392/158832892-d3744344-fad5-47cb-ac83-840469381bea.png)

Environment

  • go version: go1.18
  • operating system: linux/amd64