英文:
How to execute multiple templates in a single webpage using different interfaces in golang?
问题
请原谅我一个看起来奇怪的问题。我不确定如何用一个简洁的陈述来表达我的问题。
我在网页中有三个模板:header(页眉)、layout(布局)和footer(页脚)。
在header模板中,我有一个类别下拉菜单,并且在我的go代码中有一个包含子菜单项的字符串切片。
Categories := []string{"Holiday","IQ","Future"}
header模板的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)
布局模板的结构如下:
一些HTML代码
{{template "header"}}
更多HTML代码
{{template "footer"}}
同时使用这两个执行模板语句会给我两个不同的页眉,显然不是我想要的。
如果我从布局模板中移除header模板,视觉输出是完美的,但是当你查看HTML代码时,菜单栏位于"link rel"语句之上(记住,在布局模板中,我在{{template "header"}}之前有一些HTML代码),这显然不好。
我应该怎么做才能同时执行这两个模板,并使用它们各自的结构体?
英文:
Please forgive me for a weird looking question. I wasn't sure exactly how to state my problem in a single statement.
I have three templates in my webpage, header, layout and footer.
In the template header, I have a categories dropdown menu and I have a slice of strings with sub-menu items in my go code.
Categories := []string{"Holiday","IQ","Future"}
and the template header has following html code
<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>
so when I do a,
t,err :=template.ParseFiles("template/header.html","template/index.html","template/footer.html")
t.ExecuteTemplate(w,"header",Categories)
It gives me a nice looking header but I need to do
t.ExecuteTemplate(w,"layout",Featured)
for the main page. Layout template has the following structure
some html code
{{template "header"}}
more html code
{{template "footer"}}
Using both the execute template statements together gives me two different headers,obviously.
If I remove template header from the template layout, the visual output is perfect but when you look at the html code, the menu bar is above the "link rel" statements(remember,I had 'some html code' above {{template "header"}} in the layout template) and that is obviously not good.
What should I do so that both the templates are executed simultaneously using their respective structs?
答案1
得分: 1
我决定编辑我的头部模板,将它上面的所有内容都包括进去,并相应地修改了我的Go代码。实际上,我在头部模板上面还有一些CSS和脚本引用。由于每个页面都不一样,我只在头部包含了导航栏,但我找到了解决这个问题的方法。
我创建了一个新的结构体:
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"}}部分。现在结果看起来完美,并且按照我想要的方式工作。
英文:
I decided to edit my header template to include everything above it as well and changed my go code accordingly. I actually had some css and script references above it. Since it was going to be different for every page, I only included the nav_bar in the header but I figured out to fix this problem.
I made a new struct
type Header struct{
Css []string;
Title string;
Js []string;
Categories []string;
}
and this is part of my header template
{{range $i,$e:=.Css}}
<link rel="stylesheet" type="text/css" href="{{$e}}">
{{end}}
{{range $i,$e:=.Js}}
<script src="{{$e}}"></script>
{{end}}
I did the execute template part with the header first with the respective header interface and then another execute template with its respective interface. Also i had to remove the {{template "header"}} part from index.html. The result Looks perfect now and is working the way I want it to.
答案2
得分: 0
我遇到了类似的问题。一个明显的解决方案是将文本连接成一个模板。之前我尝试了解析、执行、解析、执行的方式,但结果并不如预期。下面是更简单的方法:
t, err := template.New("moo").Parse(header + content + footer)
t.Execute(w, data)
至少目前来说,我将所有的数据都合并到一个名为"data"的结构体中。
英文:
I had a similar problem. One obvious solution is to just concatenate the text into one template. Before I was trying to Parse > Execute > Parse > Execute... which did not work as expected. This is easier:
t, err := template.New("moo").Parse(header + content + footer)
t.Execute(w, data)
At least for now, I combine all my data into one "data" structure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论