根据主模板实际需要的情况动态解析子模板。

huangapple go评论82阅读模式
英文:

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")
	}
}

假设主模板只需要tmplAtmplB作为子模板。我如何检测它只需要这两个模板?

我不想每次添加或调整模板时都修改程序。

英文:

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) \"([^() ]*)\" ?.* ?-?}}`)

regex101

其中 partial 只是一个示例,当你遇到更复杂的情况时可以使用。你可以自行删除或添加更多关键字。

<kbd>go-playground</kbd>


其他部分,比如 CollectFiles 我认为不是那么重要,如果有人需要参考,可以查看以下链接:


然后,只需要过滤模板,找出当前文件使用的模板。


最后,代码无需更新,即可添加模板(除非你想更新站点上下文)。


我在 Github 上写了一个简单的示例,可以运行,让人们了解我想做什么。

子模板的嵌套。

如果你想处理这个问题,可以尝试使用这个函数

现在这将起作用

如果你渲染:index.gohtml,不仅包含 base.gohtml,还包含 {head.gohtmlnavbar.gohtmlfooter.gohtml}

&lt;!-- index.gohtml --&gt; 
{{- template &quot;base.gohtml&quot; . -}} &lt;!-- &#128072; --&gt; 
{{define &quot;head&quot;}}
  &lt;style&gt;h2 {background-color: yellow;}
  &lt;/style&gt;
{{end}}
{{define &quot;body&quot;}}
&lt;h2&gt;Welcome to XXX&lt;/h2&gt;
{{end}}

其中 base.gohtml

{{template &quot;head.gohtml&quot; . -}}
{{template &quot;navbar.gohtml&quot;}}
{{- block &quot;body&quot; . -}}
{{- end -}}
{{template &quot;footer.gohtml&quot;}}

最终版本

英文:

You can find all the keywords in the template in this way.

regexp.MustCompile(`{{-? ?(template|partial) \&quot;([^() ]*)\&quot; ?.* ?-?}}`)

regex101

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

&lt;!-- index.gohtml --&gt; 
{{- template &quot;base.gohtml&quot; . -}} &lt;!-- &#128072; --&gt; 
{{define &quot;head&quot;}}
  &lt;style&gt;h2 {background-color: yellow;}
  &lt;/style&gt;
{{end}}
{{define &quot;body&quot;}}
&lt;h2&gt;Welcome to XXX&lt;/h2&gt;
{{end}}

where base.gohtml

{{template &quot;head.gohtml&quot; . -}}
{{template &quot;navbar.gohtml&quot;}}
{{- block &quot;body&quot; . -}}
{{- end -}}
{{template &quot;footer.gohtml&quot;}}

The final version

huangapple
  • 本文由 发表于 2022年5月23日 17:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/72346351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定