In the application, I will use totally different query for the second query. The second query will be quite long
SELECT SIMILARITY
query. In this question, I give simple query to make it easier to understand
我需要在模板中打印来自 PostgreSQL 的数据。一切正常,但输出 HTML 有额外的 range
。
下面是 HTML 输出。您可以看到没有值的额外 range
:
<table>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
<tr>
<td>Nation</td>
<td>Nation has various meanings, and the meaning has changed over time</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
<h1>ID number:</h1>
<h3></h3>
<h3>5</h3>
下面是server.go
package main
import (
"database/sql"
"github.com/labstack/echo"
_ "github.com/lib/pq"
"html/template"
"io"
"log"
"net/http"
)
type Gallery struct {
Title, Content, Idnumber string
}
type (
Template struct {
templates *template.Template
}
)
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
return &echo.HTTPError{Error: err}
}
return nil
}
func main() {
e := echo.New()
db, err := sql.Open("postgres", "user=postgres password=apassword dbname=lesson4 sslmode=disable")
if err != nil {
log.Fatal(err)
}
t := &Template{
templates: template.Must(template.ParseFiles("public/views/testhere.html")),
}
e.Renderer(t)
e.Get("/post/:uritext", func(c *echo.Context) *echo.HTTPError {
rows, err := db.Query("SELECT title, content FROM gallery WHERE uri=$1", c.Param("uritext"))
anotherquery, err := db.Query("SELECT id AS idnumber FROM gallery WHERE uri=$1", c.Param("uritext"))
if err != nil {
log.Fatal(err)
}
gallery := []Gallery{}
for rows.Next() {
g := Gallery{}
err := rows.Scan(&g.Title, &g.Content)
if err != nil {
log.Fatal(err)
}
gallery = append(gallery, g)
}
for anotherquery.Next() {
g := Gallery{}
err := anotherquery.Scan(&g.Idnumber)
if err != nil {
log.Fatal(err)
}
gallery = append(gallery, g)
}
return c.Render(http.StatusOK, "onlytestingtpl", gallery)
})
e.Run(":4444")
}
下面是模板public/views/testhere.html
{{define "onlytestingtpl"}}
<table>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
{{range.}}
<tr>
<td>{{.Title}}</td>
<td>{{.Content}}</td>
</tr>
{{end}}
</table>
<h1>ID number:</h1>
{{range.}}
<h3>{{.Idnumber}}</h3>
{{end}}
{{end}}
我对模板中的 range
有感觉,但我不知道,因为没有错误。
最佳答案
我觉得很合适。您正在运行两个单独的查询:
rows, err := db.Query("SELECT title, content FROM gallery WHERE uri=$1", c.Param("uritext"))
anotherquery, err := db.Query("SELECT id AS idnumber FROM gallery WHERE uri=$1", c.Param("uritext"))
然后从两者创建一个 slice :
gallery = append(gallery, g)
gallery = append(gallery, g)
所以你有两行:
{ "Nation", "Nation...", 0 }
{ "", "", 5 }
这是你想要的吗?如果您希望它们合并,您只需将查询更改为:
SELECT title, content, id as idnumber FROM gallery WHERE uri=$1
如果你想要两个单独的列表,那么也许你应该有两个单独的类型:
type Gallery struct {
Title, Content string
}
type IDNumber string
构建两个单独的列表并使用组合对象进行渲染:
type Model struct {
Galleries []Gallery
IDNumbers []IDNumber
}
return c.Render(http.StatusOK, "onlytestingtpl", Model{
Galleries: galleries,
IDNumbers: idnumbers,
})
你的模板以:
结尾{{range .Galleries}}
<tr>
<td>{{.Title}}</td>
<td>{{.Content}}</td>
</tr>
{{end}}
关于sql - 戈朗 : Multiple SQL query generate extra empty {{range. }},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329793/