Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 转到导航栏当前页面的模板检查_Go - Fatal编程技术网

Go 转到导航栏当前页面的模板检查

Go 转到导航栏当前页面的模板检查,go,Go,我正在尝试根据当前页面将golang模板中的li设置为活动。从我读到的内容来看,您只能执行{{if.scoreheader}}来检查变量的存在性。还有别的办法吗 <div class="col-md-3"> <ul class="nav nav-pills nav-stacked"> {{range $id, $name := .test}} {{if $name == .scoreheader}}

我正在尝试根据当前页面将golang模板中的li设置为活动。从我读到的内容来看,您只能执行{{if.scoreheader}}来检查变量的存在性。还有别的办法吗

    <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>

    {{range$id,$name:=.test} {{如果$name==.scoreheader}
  • {{else}
  • {{end}
  • {{end}

您可以使用
eq
功能,如中所述:

还有一组二进制比较运算符定义为 职能:

eq返回arg1==arg2的布尔真值

因此,您的if声明将是:

{{if eq $name $.scoreheader}}

必须做:{{$scoreheader:=.scoreheader}}之前{{{if eq$name$scoreheader}}但它可以工作。非常感谢。啊,这个。在循环中具有内部值。我改为
{if eq$name$.scoreheader}
,这也应该可以用。$是指的初始值。(点)。