douyi0219 2019-06-06 01:14
浏览 14

如何在文件范围内跳过一个特定的文件名

I'm working on some blog code (written by someone else) which lists all posts into the main index.html file. I want to exclude one file (welcome.md) from this listing. I believe this is the relevant code that does this -

{{$l := len .}}
{{range $i, $e := .}}         
<h3><a href="/{{$e.Title | slug}}.html">{{$e.Title}}</a></h3>

Is it possible?

Update - Here is my full code, I left some out of the above -

{{define "title"}}
  Test
{{end}}

{{define "content"}}
<h1>Heading</h1>

{{$l := len .}}
      {{range $i, $e := .}}
      {{- if ne $e.Title "welcome" -}}        
            <h3><a href="/{{$e.Title | slug}}.html">{{$e.Title}}</a></h3>
            {{- end }}
            <small>
              <em>
              {{$e.Written.Format "Jan 2, 2006"}}&nbsp;
              Tags:  {{range $e.Tags}}
              <a href="/tags/{{. | slug}}.html" title="Posts Tagged {{.}}">{{.}}</a>&nbsp;
                {{end}}
              </em>
            </small>
            {{(printf "%s </br><small>[Read more](/%s.html)</small>" ($e.Content | summary) (.Title | slug)) | html}}

{{end}}
{{end}}
  • 写回答

2条回答 默认 最新

  • dqcuq4138 2019-06-06 04:42
    关注

    You can use {{if ...}} in templates. Combine with the ne function (for "not equal"):

    {{range $i, $e := .}}
      {{- if ne $e.Title "welcome" -}}
    <h3><a href="/{{$e.Title}}.html">{{$e.Title}}</a></h3>
      {{- end }}
    {{ end }}
    

    playground example

    However, it feels like maybe you can make it more general purpose if you have control over the data model. Perhaps a flag on each post for ExcludeFromIndex or something like that:

    {{- if !$e.ExcludeFromIndex -}}
    

    That way if you add more "special" pages you won't need to keep adding if statements for each one. Just an idea.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)