duano3557
duano3557
2014-09-09 13:19

ParseGlob:在目录中递归解析所有模板的模式是什么?

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳
Template.ParseGlob("*.html") //fetches all html files from current directory.
Template.ParseGlob("**/*.html") //Seems to only fetch at one level depth

Im not looking for a "Walk" solution. Just want to know if this is possible. I don't quite understand what "pattern" this expects. if i can get an explanation about the pattern used by ParseGlob that would be great too.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanju7199 duanju7199 7年前

    The code text/template/helper.go mentions

     // The pattern is processed by filepath.Glob and must match at least one file.
    

    filepath.Glob() says that "the syntax of patterns is the same as in Match"

    Match returns true if name matches the shell file name pattern.

    The implementation of Match() doesn't seem to treat '**' differently, and only consider '*' as matching any sequence of non-Separator characters.
    That would mean '**' is equivalent to '*', which in turn would explain why the match works at one level depth only.

    点赞 评论 复制链接分享
  • doujiangqu2823 doujiangqu2823 3年前

    So, since the ParseGlob can't load templates recursively we have to use path/filepath.Walk function. But this way gives more opportunities.

    https://gist.github.com/logrusorgru/abd846adb521a6fb39c7405f32fec0cf

    点赞 评论 复制链接分享

为你推荐