Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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
Loops 雨果&x2013;如何根据计数器添加不同的类名_Loops_Templating_Hugo - Fatal编程技术网

Loops 雨果&x2013;如何根据计数器添加不同的类名

Loops 雨果&x2013;如何根据计数器添加不同的类名,loops,templating,hugo,Loops,Templating,Hugo,使用Hugo,我需要在前几篇文章中添加唯一的类名。如何做到这一点 我的代码显然不起作用 // if the first post {{ if eq .Site.GetPage 1 }} {{ $classname := "class-one" }} // else the second post {{ elseif eq .Site.GetPage 2 }} {{ $classname := "class-two" }} // else the third post {{ els

使用Hugo,我需要在前几篇文章中添加唯一的类名。如何做到这一点

我的代码显然不起作用

// if the first post
{{ if eq .Site.GetPage 1 }}
    {{ $classname := "class-one" }}
// else the second post
{{ elseif eq .Site.GetPage 2 }}
    {{ $classname := "class-two" }}
// else the third post
{{ elseif eq .Site.GetPage 3 }}
    {{ $classname := "class-three" }}
{{ else }}
{{ end }}

<li class="{{ $classname }}">
    …
</li>
//如果第一篇文章
{{if eq.Site.GetPage 1}
{{$classname:=“一类”}
//否则第二个职位
{{elseif eq.Site.GetPage 2}
{{$classname:=“第二类”}
//否则第三个职位
{{elseif eq.Site.GetPage 3}
{{$classname:=“第三类”}
{{else}
{{end}

  • 也许有更有效的方法,但这是可行的:

    {{ $paginator := .Paginate (where .Data.Pages.ByDate.Reverse "Type" "post") }}
    
    {{ range $index, $element := $paginator.Pages }}
    
    {{ if (eq $index 0) }}
    {{ $.Scratch.Set "classname" "class-one" }}
    {{ else if (eq $index 1) }}
    {{ $.Scratch.Set "classname" "class-two" }}
    {{ else }}
    {{ $.Scratch.Set "classname" "" }}
    {{ end }}
    
    <li class="{{ $.Scratch.Get "classname" | safeHTML }}">
        …
    </li>
    
    {{ end }}
    
    {{$paginator:=.Paginate(其中.Data.Pages.ByDate.Reverse“Type”“post”)}
    {{range$index,$element:=$paginator.Pages}
    {{if(eq$index 0)}
    {{$.Scratch.Set“classname”“class one”}
    {{else if(eq$index 1)}
    {{$.Scratch.Set“classname”“class two”}
    {{else}
    {{$.Scratch.Set“classname”“”}
    {{end}
    
  • {{end}