douzhi4991 2016-12-23 14:55
浏览 24
已采纳

如何在Golang中使用不同的界面在一个网页中执行多个模板?

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条回答 默认 最新

  • donglinyi4313 2016-12-23 18:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog