如何在Go中逐步编写HTML模板

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

How to write incrementally to html template in go

问题

我正在构建一个名为Mavgo Flight的基于Web的数据浏览器。我希望从sqlite中获取的大型表能够连续打印,而不是默认的只有在所有数据可用时才打印的行为。我尝试了按行运行模板的方法,但失败了。

func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
    cols, err := result.Columns()
    if err != nil {
        log.Println(err, "renderHTMLTable")
        return
    }
    tmpl, err := template.ParseFiles("./templates/2d.html")
    if err != nil {
        log.Println("template failed", err)
        return
    }
    data := HTMLTable{}
    data.Cols = cols
    for result.Next() {
        cols, err := result.SliceScan()
        if err != nil {
            log.Println(w, err)
            break
        }
        s := make([]string, len(cols))
        for i, v := range cols {
            s[i] = fmt.Sprint(v)
        }
        tmpl.Execute(w, s)
    }
}

以上是你提供的代码。

英文:

I'm building a web based data browser called Mavgo Flight.
I want large tables from sqlite to print continuously instead of the default behavior of only printing when all the data is available. I tried running the template per row of data which fails.

func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
	cols, err := result.Columns()
	if err != nil {
		log.Println(err, "renderHTMLTable")
		return
	}
	tmpl, err := template.ParseFiles("./templates/2d.html")
	if err != nil {
		log.Println("template failed", err)
		return
	}
	data := HTMLTable{}
	data.Cols = cols
	for result.Next() {
		cols, err := result.SliceScan()
		if err != nil {
			log.Println(w, err)
			break
		}
		s := make([]string, len(cols))
		for i, v := range cols {
			s[i] = fmt.Sprint(v)
		}
		tmpl.Execute(w, s)
	}
}

答案1

得分: 0

我放弃了聪明才智,完全按照Cerise的建议去做了
逐行编写的函数:

func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
    cols, err := result.Columns()
    if err != nil {
        log.Println(err, "renderHTMLTable")
        return
    }
    head, err := template.ParseFiles("./templates/head.html")
    if err != nil {
        log.Println("template failed", err)
        return
    }
    row, err := template.ParseFiles("./templates/row.html")
    if err != nil {
        log.Println("template failed", err)
        return
    }
    foot := `  </tbody>
    </table>
    </div>
    
    </body>
    </html>`
    head.Execute(w, cols)
    s := make([]string, len(cols))
    for result.Next() {
        values, err := result.SliceScan()
        if err != nil {
            log.Println(w, err)
            break
        }

        for i, v := range values {
            s[i] = fmt.Sprint(v)
        }
        row.Execute(w, s)
    }
    fmt.Fprint(w, foot)
}
英文:

I gave up on being clever did exactly what Cerise suggested
The function that writes rows incrementally:

func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
cols, err := result.Columns()
if err != nil {
	log.Println(err, &quot;renderHTMLTable&quot;)
	return
}
head, err := template.ParseFiles(&quot;./templates/head.html&quot;)
if err != nil {
	log.Println(&quot;template failed&quot;, err)
	return
}
row, err := template.ParseFiles(&quot;./templates/row.html&quot;)
if err != nil {
	log.Println(&quot;template failed&quot;, err)
	return
}
foot := `  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;`
head.Execute(w, cols)
s := make([]string, len(cols))
for result.Next() {
	values, err := result.SliceScan()
	if err != nil {
		log.Println(w, err)
		break
	}

	for i, v := range values {
		s[i] = fmt.Sprint(v)
	}
	row.Execute(w, s)
}
fmt.Fprint(w, foot)

}

huangapple
  • 本文由 发表于 2022年12月27日 12:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74925673.html
匿名

发表评论

匿名网友

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

确定