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
Add new method LoadHTMLFilesRecursively for recursively load template files in a folder #1296
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1296 +/- ##
==========================================
+ Coverage 98.41% 98.41% +<.01%
==========================================
Files 34 34
Lines 1762 1772 +10
==========================================
+ Hits 1734 1744 +10
Misses 23 23
Partials 5 5
Continue to review full report at Codecov.
|
@thinkerou Please help to review this PR. Thanks. |
|
||
```go | ||
func main() { | ||
router := gin.Default() | ||
router.LoadHTMLGlob("templates/*") | ||
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") | ||
|
||
// load recursively all .html files in "public" folder | ||
//router.LoadHTMLFilesRecursively("public", []string{".html"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add one complete example which should be copied and debug by gin user.
// LoadHTMLFilesRecursively loads recursively a slice of HTML files mathing an extension | ||
// and associates the result with HTML renderer. | ||
func (engine *Engine) LoadHTMLFilesRecursively(folder string, extensionsAllowed []string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the empty line.
sort.Strings(extensionsAllowed) | ||
|
||
filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { | ||
extension := strings.ToLower(filepath.Ext(path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly you need to use lower extensionsAllowed
extension := strings.ToLower(filepath.Ext(path)) | ||
indexFound := sort.SearchStrings(extensionsAllowed, extension) | ||
if indexFound < len(extensionsAllowed) && extensionsAllowed[indexFound] == extension { | ||
files = append(files, path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are have sort
and search
two steps.
why not use the follow code?
for _, v := range extensionsAllowed {
if strings.ToLower(v) == extension {
files = append(files, path)
}
}
moreover, sort.SearchStrings
maybe have problem, see golang/go#25382
@math-nao any updates? |
ref: https://github.com/gin-gonic/gin#html-rendering |
@math-nao please fix conflict and then I merged it, thanks! |
https://stackoverflow.com/questions/26809484/how-to-use-double-star-glob-in-go golang's |
Load new html templates to the exists HTMLRender. This method won't create a new template instance. So we can load templates from different path and use golang template `define` tag to give the templates proper name. Golang's `filepath.Glob` doesn't support glob dir recursively. This method may be helpful. May be related with gin-gonic#1296.
Why this thread stoped..... |
Currently, Gin doesn't handle recursive load of template files in a folder.
This pull request adds a new method called
LoadHTMLFilesRecursively
to load all files matching a list of extension in a specific folder.