1 回答

TA贡献1911条经验 获得超7个赞
您可以通过这种方式找到模板中的所有关键字。
regexp.MustCompile(`{{-? ?(template|partial) \"([^() ]*)\" ?.* ?-?}}`)
当您遇到更复杂的情况时,哪里partial
只是您使用的示例。您可以删除它或自行添加更多关键字。
其他部分,比如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"}}
- 1 回答
- 0 关注
- 90 浏览
添加回答
举报