go - 如何覆盖文件但跳过一个特定的文件名
问题描述
我正在编写一些博客代码(由其他人编写),它将所有帖子列出到主 index.html 文件中。我想从这个列表中排除一个文件(welcome.md)。我相信这是执行此操作的相关代码-
{{$l := len .}}
{{range $i, $e := .}}
<h3><a href="/{{$e.Title | slug}}.html">{{$e.Title}}</a></h3>
可能吗?
更新 - 这是我的完整代码,我在上面遗漏了一些 -
{{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}}
解决方案
您可以{{if ...}}
在模板中使用。结合ne
函数(对于“不等于”):
{{range $i, $e := .}}
{{- if ne $e.Title "welcome" -}}
<h3><a href="/{{$e.Title}}.html">{{$e.Title}}</a></h3>
{{- end }}
{{ end }}
但是,如果您可以控制数据模型,感觉也许您可以使其更通用。也许每个帖子上都有一个标志ExcludeFromIndex
或类似的东西:
{{- if !$e.ExcludeFromIndex -}}
这样,如果您添加更多“特殊”页面,则无需为每个页面继续添加 if 语句。只是一个想法。
推荐阅读
- apache - 子域的自定义 http.conf
- c++ - 读取 obj 文件!(指数)
- file - Wave文件混淆
- mysql - DOCKER 和 WordPress:建立数据库连接时出错
- hibernate - Hibernate @Filter 无法使用 FetchType.LAZY 处理 Child
- php - PHP CURL 问题 | 标头上下文类型未发送
- java - MapStruct 子实体
- javascript - 无法 POST 到 Express 路由器(404 错误)
- r - 用 ggplot2 绘制 Shapefile
- javascript - 通过断点而不是多个 else if 语句查找索引