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

Add new method LoadHTMLFilesRecursively for recursively load template files in a folder #1296

Closed
wants to merge 7 commits into from

Conversation

math-nao
Copy link

@math-nao math-nao commented Mar 22, 2018

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.

@codecov
Copy link

codecov bot commented Mar 23, 2018

Codecov Report

Merging #1296 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
gin.go 98.15% <100%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6d913fc...628f0a1. Read the comment docs.

@appleboy
Copy link
Member

@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"})
Copy link
Member

@thinkerou thinkerou May 14, 2018

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) {

Copy link
Member

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))
Copy link
Member

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)
Copy link
Member

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

@appleboy
Copy link
Member

@math-nao any updates?

@thinkerou
Copy link
Member

thinkerou commented May 17, 2018

ref: https://github.com/gin-gonic/gin#html-rendering
router.LoadHTMLGlob("templates/**/*")

@thinkerou
Copy link
Member

@math-nao please fix conflict and then I merged it, thanks!

@thinkerou thinkerou modified the milestones: 1.4, 1.5 Mar 1, 2019
@ganlvtech
Copy link
Contributor

ref: https://github.com/gin-gonic/gin#html-rendering
router.LoadHTMLGlob("templates/**/*")

https://stackoverflow.com/questions/26809484/how-to-use-double-star-glob-in-go

golang's filepath.Glob doesn't handle templates/**/* like other language. It works same as template/*/*.

ganlvtech added a commit to ganlvtech/gin that referenced this pull request Apr 23, 2019
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.
@unlikezy
Copy link

Why this thread stoped.....

@thinkerou thinkerou removed this from the 1.5 milestone Oct 31, 2019
@thinkerou
Copy link
Member

@math-nao thanks a lot, I will close the pr, and please see #2328

@thinkerou thinkerou closed this Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants