Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load HTML templates from a separate module #4018

Open
josez1984BT opened this issue Jul 26, 2024 · 1 comment
Open

Load HTML templates from a separate module #4018

josez1984BT opened this issue Jul 26, 2024 · 1 comment

Comments

@josez1984BT
Copy link

Description

I have a Go module that's in a private repository. This Go module has a function "SetupRouter" that takes in a *gin.Engine, and has html templates that it attempts to serve via it's own routes. The Go module loads the html templates fine when ran as a standalone program, but it crashes when I import the module into another Go app.

How to reproduce

Module file dir

  • go.mod
  • go.sum
  • web
    • views
      • pages
        • index.html

Module code to load html

package webview

import "github.com/gin-gonic/gin"

func SetupRouter(r *gin.Engine) {
  r.LoadHTMLGlob("web/views/**/*")
}

Main app

package main

import (
  "path/to/module/webview/"
  "import "github.com/gin-gonic/gin"
)

func main() {
  r := gin.New()
  webview.SetupRouter(r)
}

Expectations

The html files are loaded successfully.

Actual result

panic: html/template: pattern matches no files: `web/views/**/*`

Environment

  • go version: 1.22
  • gin version (or commit ref): v1.10.0
  • operating system: macos
@JimChenWYU
Copy link

This error is from html/template

package webview

func SetupRouter(r *gin.Engine) {
  // the current path is `path/to/module` not root path
  r.LoadHTMLGlob("web/views/**/*")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants