检查当前页面的Go模板,用于导航栏。

huangapple go评论65阅读模式
英文:

Go template checking for current page for nav bar

问题

我正在尝试在golang模板中设置一个li元素,根据当前页面来设置为活动状态。根据我所了解,你只能使用{{if .scoreheader}}来检查变量的存在性。是否有其他方法可以解决这个问题?

英文:

I'm trying to set a li in a golang template to active based on the current page. From what I've read you can only do {{if .scoreheader}} to check existence of a variable. Is there another way around this?

	<div class="col-md-3">
      <ul class="nav nav-pills nav-stacked">

		{{range $id, $name := .test}}
			{{if $name == .scoreheader}}
			<li class="active">
			{{else}}
			<li>
			{{end}}
			<li><a href="/app/index/?company={{$id}}">{{$name}}</a></li>
		{{end}}
      </ul>
    </div>

答案1

得分: 1

你可以使用eq函数,如text/template中所解释的那样:

还有一组作为函数定义的二进制比较运算符:

eq 返回 arg1 == arg2 的布尔值

因此,你的if语句应该是:

{{if eq $name $.scoreheader}}
英文:

You can use the eq function as explained in text/template:

> There is also a set of binary comparison operators defined as
> functions:
>
> eq Returns the boolean truth of arg1 == arg2

So your if-statement would be:

{{if eq $name $.scoreheader}}

huangapple
  • 本文由 发表于 2014年11月10日 15:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/26839056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定