基于已加载模板的CSS类

I've got this bootstrap nav in my _base.html template like this:

<ul class="nav navbar-nav">
   <li><a href="/" class="">Home</a></li>
   <li><a href="/blog/">Blog</a></li>
</ul>

Using Golang I want to add a

class="active"

to the corresponding list-item.

I've read the html/template docs and articles like thisone, but it appears to me that I have to write a golang function that adds

class="active"

to every correspondending corresponding list-item. But somehow still I think it would be cleaner if I could just add something like

<ul>
    <li{{ if .template = "index.html" }} class="active"{{ end }}><a href="/">Home</a></li>
    <li{{ if .template = "blog.html" }} class="active"{{ end }}><a href="/blog/">Blog</a></li>
</ul>

or something like that. I remember Rob Pike saying Golang should be doing all the calculations for you, but why is there an "if" statement in the html/template-package?

查看全部
dongtu1789
dongtu1789
2013/08/21 08:15
  • css
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复