Golang: .Scan() 忽略类型 template.HTML

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

Golang: .Scan() ignoring type template.HTML

问题

所以现在,.Scan() 方法不接受 template.HTML 类型;它完全忽略它并且不输出任何 HTML。这是我卡住并且不确定的地方。

如果我将其作为字符串传递,它可以工作,但是 HTML 会以转义字符输出:

<h2>metus congue

如果有其他的解决方案,那将很好。

代码是一个单页应用-完整页面在这里:http://pastebin.com/E4jXiv6p

结构体:

type Pages struct {
    Name        string
    Url         string
    Title       string
    Description string
    H1          string
    Hero        string
    Contents    template.HTML
    Sidebar     string
    Page_list   [][]string
}

查询页面:

func db_query_pages(db *sql.DB, err error, page_list [][]string) {

    var name, url, title, description, h1, hero, sidebar string

    var contents template.HTML

    rows, err := db.Query("SELECT * FROM pages")

    for rows.Next() {

        rows.Scan(&name, &url, &title, &description, &h1, &hero, &contents, &sidebar)

        page := &Pages{

            Name:        name,
            Url:         url,
            Title:       title,
            Description: description,
            H1:          h1,
            Hero:        hero,
            Contents:    contents,
            Sidebar:     sidebar,
            Page_list:   page_list,
        }

        render_page(page)

    }

}

渲染页面:

func render_page(new_page *Pages) {

    http.HandleFunc(new_page.Url,
        func(w http.ResponseWriter, r *http.Request) {

            templates.ExecuteTemplate(w, "main", new_page)

        })

}

HTML:

{{define "content"}}

    <h1>{{.H1}}</h1>

    <img class="margin-bottom-md block common-border padding-md" src="{{.Hero}}">

    {{.Contents}}

{{end}}
英文:

So right now .Scan() is not taking in the type template.HTML; it completely ignores it and outputs nothing to the HTML. This is the one thing I am stuck on and uncertain about.

If I pass it as a string it works, but the HTML outputs as escaped characters

<h2>metus congue

If there is an alternative solution that would be neat.

The code is a one pager- full page here: http://pastebin.com/E4jXiv6p

The Struct

type Pages struct {
    Name        string
    Url         string
    Title       string
    Description string
    H1          string
    Hero        string
    Contents    template.HTML
    Sidebar     string
    Page_list   [][]string
}

Querying pages

func db_query_pages(db *sql.DB, err error, page_list [][]string) {

	var name, url, title, description, h1, hero, sidebar string

	var contents template.HTML

	rows, err := db.Query("SELECT * FROM pages")

	for rows.Next() {

		rows.Scan(&name, &url, &title, &description, &h1, &hero, &contents, &sidebar)

		page := &Pages{

			Name:        name,
			Url:         url,
			Title:       title,
			Description: description,
			H1:          h1,
			Hero:        hero,
			Contents:    contents,
			Sidebar:     sidebar,
			Page_list:   page_list,
		}

		render_page(page)

	}

}

Rendering pages

func render_page(new_page *Pages) {

	http.HandleFunc(new_page.Url,
		func(w http.ResponseWriter, r *http.Request) {

			templates.ExecuteTemplate(w, "main", new_page)

		})

}

HTML

{{define "content"}}

	<h1>{{.H1}}</h1>

	<img class="margin-bottom-md block common-border padding-md" src="{{.Hero}}">

	{{.Contents}}

{{end}}

答案1

得分: 1

你无法对html.Template进行编组(Marshal)或解组(Unmarshal)。

你需要在数据库中存储模板名称的引用、原始模板数据或已渲染的 HTML。

英文:

You can't Marshal or Unamrshal an html.Template.

You either need to store the a reference to the template name, the raw template data, or the rendered html in the DB.

huangapple
  • 本文由 发表于 2015年10月24日 01:11:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/33307947.html
匿名

发表评论

匿名网友

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

确定