请原谅我一个奇怪的问题。我不确定如何在一个声明中陈述我的问题。
我的网页,标题,布局和页脚中有三个模板。
在模板标题中,我有一个类别下拉菜单,我的go代码中有一些包含子菜单项的字符串。
Categories := []string{"Holiday","IQ","Future"}
,模板标题包含以下html代码
<div class="ui dropdown item">
<i class="browser icon"></i>
Categories
<i class="dropdown icon"></i>
<div class="menu">
{{range $i,$e:= .}}
<a class="item"><i class="hashtag icon"></i>{{$e}}</a>
{{end}}
</div>
</div>
所以当我这样做时,
t,err :=template.ParseFiles("template/header.html","template/index.html","template/footer.html")
t.ExecuteTemplate(w,"header",Categories)
它给了我一个漂亮的标题,但我需要做
t.ExecuteTemplate(w,"layout",Featured)
表示主页面。布局模板具有以下结构
some html code
{{template "header"}}
more html code
{{template "footer"}}
显然,同时使用两个执行模板语句会给我两个不同的标题。
如果我从模板布局中删除模板标题,视觉输出是完美的,但是当你查看html代码时,菜单栏位于&#34; link rel&#34;陈述(请记住,我在布局模板中有{{template&#34; header&#34;}}以上的某些HTML代码),这显然不太好。
我应该怎么做才能使用各自的结构同时执行两个模板?
答案 0 :(得分:1)
我决定编辑我的标题模板以包含它上面的所有内容并相应地更改我的go代码。我实际上有一些css和脚本引用它。由于每个页面都不同,我只在标题中包含了nav_bar,但我想出来解决这个问题。
我制作了一个新结构
type Header struct{
Css []string;
Title string;
Js []string;
Categories []string;
}
这是我的标题模板的一部分
{{range $i,$e:=.Css}}
<link rel="stylesheet" type="text/css" href="{{$e}}">
{{end}}
{{range $i,$e:=.Js}}
<script src="{{$e}}"></script>
{{end}}
我先执行了带有标题的执行模板部分,然后是相应的标题接口,然后是另一个带有各自接口的执行模板。我还必须从index.html中删除{{template“header”}}部分。结果现在看起来很完美,并且按照我想要的方式工作。