在Echo框架中,从PostgreSQL数据库中获取的变量在模板中没有输出值。

huangapple go评论76阅读模式
英文:

Go: Variable from PostgreSQL in template not output value (Echo framework)

问题

许多代码取自Martini示例,但这里使用的是Echo框架。

我可以让它在Martini中工作,但在Echo中不行。

server.go:

package main

import (
    "database/sql"
    "github.com/labstack/echo"
    _ "github.com/lib/pq"
    "html/template"
    "io"
    "log"
    "net/http"
)

type Book struct {
    Title, Author, Description string
}

type (
    Template struct {
        templates *template.Template
    }
)

func (t *Template) Render(w io.Writer, name string, data interface{}) error {
    return t.templates.ExecuteTemplate(w, name, data)
}

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/:idnumber", func(c *echo.Context) {

        rows, err := db.Query("SELECT title, author, description FROM books WHERE id=$1", c.Param("idnumber"))
        if err != nil {
            log.Fatal(err)
        }

        books := []Book{}

        for rows.Next() {
            b := Book{}
            err := rows.Scan(&b.Title, &b.Author, &b.Description)

            if err != nil {
                log.Fatal(err)
            }

            books = append(books, b)
        }

        c.Render(http.StatusOK, "onlytestingtpl", books)
    })

    e.Run(":4444")
}

public/views/testhere.html:

{{define "onlytestingtpl"}}Book title is {{.Title}}. Written by {{.Author}}. The book is about {{.Description}}.{{end}}

我无法找出问题所在,因为没有错误消息,也没有关于该框架的SQL文档。运行时,它输出:

Book title is

(变量没有输出值)

英文:

Many codes taken from Martini example, but this using Echo framework.

I can make it works in Martini but not in Echo.

server.go:

package main

import (
	"database/sql"
	"github.com/labstack/echo"
	_ "github.com/lib/pq"
	"html/template"
	"io"
	"log"
	"net/http"
)

type Book struct {
	Title, Author, Description string
}

type (
	Template struct {
		templates *template.Template
	}
)

func (t *Template) Render(w io.Writer, name string, data interface{}) error {
	return t.templates.ExecuteTemplate(w, name, data)
}

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/:idnumber", func(c *echo.Context) {

		rows, err := db.Query("SELECT title, author, description FROM books WHERE id=$1", c.Param("idnumber"))
		if err != nil {
			log.Fatal(err)
		}

		books := []Book{}

		for rows.Next() {
			b := Book{}
			err := rows.Scan(&b.Title, &b.Author, &b.Description)

			if err != nil {
				log.Fatal(err)
			}

			books = append(books, b)
		}

		c.Render(http.StatusOK, "onlytestingtpl", books)
	})

	e.Run(":4444")
}

public/views/testhere.html:

{{define "onlytestingtpl"}}Book title is {{.Title}}. Written by {{.Author}}. The book is about {{.Description}}.{{end}}

I cannot figure out since no error message, and no SQL documentation of this framework. When running, it gives:

Book title is

(The variable not output value)

答案1

得分: 1

如我在评论中提到的,你不应该执行一个接受一个包含结构体切片的结构体的模板。要么在你的模板中使用{{range}},要么执行以下操作:

c.Render(http.StatusOK, "onlytestingtpl", books[0])
英文:

As I mentioned in a comment, you shouldn't execute a template that takes a struct with a slice of structs. Either use {{range}} in your template, or do

c.Render(http.StatusOK, "onlytestingtpl", books[0])

huangapple
  • 本文由 发表于 2015年5月1日 15:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/29982966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定