英文:
Dynamically parse sub-templates based on those actually required by the main template
问题
假设我有很多带有子模板的模板,那么如何根据template
管道中所需的子模板来解析sub-templates呢?
我的想法是读取当前要渲染的模板,并找出它使用了哪些模板,但我不知道如何做到这一点,也许可以使用正则表达式吗?
PS:答案不必考虑子模板的多级嵌套。
示例
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")
}
}
假设主模板只需要tmplA
和tmplB
作为子模板。我如何检测它只需要这两个模板?
我不想每次添加或调整模板时都修改程序。
英文:
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.
答案1
得分: 1
你可以通过以下方式在模板中找到所有的关键字。
regexp.MustCompile(`{{-? ?(template|partial) \"([^() ]*)\" ?.* ?-?}}`)
其中 partial
只是一个示例,当你遇到更复杂的情况时可以使用。你可以自行删除或添加更多关键字。
<kbd>go-playground</kbd>
其他部分,比如 CollectFiles
我认为不是那么重要,如果有人需要参考,可以查看以下链接:
然后,只需要过滤模板,找出当前文件使用的模板。
最后,代码无需更新,即可添加模板(除非你想更新站点上下文)。
我在 Github 上写了一个简单的示例,可以运行,让人们了解我想做什么。
子模板的嵌套。
如果你想处理这个问题,可以尝试使用这个函数
现在这将起作用
如果你渲染:index.gohtml
,不仅包含 base.gohtml
,还包含 {head.gohtml
,navbar.gohtml
,footer.gohtml
}
<!-- index.gohtml -->
{{- template "base.gohtml" . -}} <!-- 👈 -->
{{define "head"}}
<style>h2 {background-color: yellow;}
</style>
{{end}}
{{define "body"}}
<h2>Welcome to XXX</h2>
{{end}}
其中 base.gohtml
{{template "head.gohtml" . -}}
{{template "navbar.gohtml"}}
{{- block "body" . -}}
{{- end -}}
{{template "footer.gohtml"}}
英文:
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.
<kbd>go-playground</kbd>
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" . -}} <!-- 👈 -->
{{define "head"}}
<style>h2 {background-color: yellow;}
</style>
{{end}}
{{define "body"}}
<h2>Welcome to XXX</h2>
{{end}}
where base.gohtml
{{template "head.gohtml" . -}}
{{template "navbar.gohtml"}}
{{- block "body" . -}}
{{- end -}}
{{template "footer.gohtml"}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论