donglin9068 2015-10-23 17:11
浏览 35
已采纳

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

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条回答 默认 最新

  • duanqiu9104 2015-10-23 17:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题