Suppose I have a large number of templates with sub-templates, then how do I parse sub-templates based on those required by template
pipelines in the?
My idea is to read the current template to be rendered and find out which templates it uses, but I don't know how to do that, perhaps with regular expressions?
PS: answers don't have to consider multi-level nesting of sub-template.
Example
package main
import (
"html/template"
"path/filepath"
)
func CollectFiles(dir string, excludeList []string) (fileList []string, err error) {
// ...
return
}
func main() {
filePathList, _ := CollectFiles("dir/src", []string{".md"})
for _, curFile := range filePathList {
_, _ = template.New(filepath.Base(curFile)).
ParseFiles(curFile, "tmplA", "tmplB", "...", "tmplN")
}
}
Suppose the main template only needs tmplA
and tmplB
as sub-templates. How can I detect that it only requires those two?
I don't want to change the program every time a new template is added or adjusted.
CodePudding user response:
You can find all the keywords in the template in this way.
regexp.MustCompile(`{{-? ?(template|partial) \"([^() ]*)\" ?.* ?-?}}`)
where partial
is just an example for you to use when you have more complicated situations. You can delete it or add more keywords by yourself.
The other parts, such as the CollectFiles
I think are not so important, if someone needs to refer to the following
And then just need to filter templates to find out what templates the current file uses.
Finally, the code without to update whenever a template is added. (except you want to update the site context)
I wrote a simple example on Github that can run so that people can know what I want to do.
Nesting of sub-template.
If you want to handle this. try to use this function
Now this will work
If you render: index.gohtml
not only base.gohtml
include but also {head.gohtml
, navbar.gohtml
, footer.gohtml
} are included
<!-- index.gohtml -->
{{- template "base.gohtml" . -}} <!--