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"}}
Tags: {{range $e.Tags}}
<a href="/tags/{{. | slug}}.html" title="Posts Tagged {{.}}">{{.}}</a>
{{end}}
</em>
</small>
{{(printf "%s </br><small>[Read more](/%s.html)</small>" ($e.Content | summary) (.Title | slug)) | html}}
{{end}}
{{end}}
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 }}
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.
OK, I had to remove the hyphens and move {{end}} down to the bottom like so -
{{$l := len .}}
{{range $i, $e := .}}
{{ if ne $e.Title "Welcome" }}
<h3><a href="/{{$e.Title | slug}}.html">{{$e.Title}}</a></h3>
<small>
<em>
{{$e.Written.Format "Jan 2, 2006"}}
Tags: {{range $e.Tags}}
<a href="/tags/{{. | slug}}.html" title="Posts Tagged {{.}}">{{.}}</a>
{{end}}
</em>
</small>
{{(printf "%s </br><small>[Read more](/%s.html)</small>" ($e.Content | summary) (.Title | slug)) | html}}
{{ end }}
{{end}}
{{end}}