'Dynamically parse sub-templates based on those actually required by the main template

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source