Caching templates

How do I read a cached template’s HTML markup using template.Must(template.ParseFiles("layout.html","1.html")).Execute()…?

Hi, @Tigran_Kashapov,

If by “cached template’s HTML markup,” you mean the resulting HTML produced by executing a template, then based on the (*html/template.Template).Execute function’s signature:

func (t *Template) Execute(wr io.Writer, data interface{}) error

The first parameter is any type that implements the io.Writer interface:

type Writer interface {
    Write(p []byte) (n int, err error)

One such example is *os.File, so you could create an *os.File with os.Create, or open an existing one for writing with os.OpenFile. You could even use os.Stdout to write the template HTML to standard output.

Another thing you could do is create a bytes.Buffer and use that when calling Execute to write the HTML into a buffer that you can then turn into a string by calling (*bytes.Buffer).String.

Okay, thanks. I’m gonna try the second way.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.