Trying to Save Template to File

Hello everyone! I have recently been experimenting with web development in Go and wanted to create a sort of “page template form” that someone could use to create a page without needing to dive into the code and edit it themselves.

I’m doing this by using a template to insert the form variables into. However, when I try to execute the template into a file, it doesn’t seem to change the file at all.

Here is my Go code:


import (
    "html/template"
    "net/http"
    "os"
    "io"
    "log"
)

type PageDetails struct {
	Content string
	Submitted bool
}

func main() {
    tmpl := template.Must(template.ParseFiles("page_template_1_form.html"))
    f, _ := os.Create("lesson.html")
    p, _ := os.Open("page_template_1_form.html")
    io.Copy(f, p)
    pageFile, err := template.ParseFiles("lesson.html")
    if (err != nil) {
    	log.Fatal("lesson not parsed correctly")
    }
    var details PageDetails;
    http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if r.Method != http.MethodPost {
            tmpl.Execute(w, nil)
            return
        }

        details = PageDetails{
            Content: r.FormValue("content"),
            Submitted: true,
        }
        f, err = os.OpenFile("lesson.html", os.O_WRONLY|os.O_APPEND, 0600)
        if (err != nil) {
        	log.Fatal("lesson was not able to be reopened")
        }
        //tmpl.Execute(w, details)
        err = pageFile.Execute(f, details)
        if (err != nil) {
        	log.Print("error is: ", err)
        }
    })
    err = f.Close()
    if (err != nil) {
    	log.Print("file could not be closed correctly: ", err)
    }

    http.ListenAndServe(":8080", nil)
}

Here is my page_template_1_form.html code:

<html>
	<head>
		<style>
			#layout div {
				background-color: white;
				border: solid 5px #003300;
				border-radius: 3px;
			}
		</style>
	</head>
	<body>
		{{if .Submitted}}
		<div id="layout" style="display: grid; grid-template-rows: repeat(10, 1fr); grid-template-columns: repeat(8, 1fr); grid-gap: 1%; width: 50vw; height: 100vh; margin: 0 auto; padding: 1%; background-color: green; grid-template-areas: 
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
		'content content content content content content content content'
				         '. . . footer footer . image image'">
			<div style="grid-area: content">{{.Content}}</div>
			<div style="grid-area: footer"></div>
			<img src="assets/graphics/Frog_1.svg" style="grid-row: -3/span 2; grid-column: -3/span 2">
		</div>
		{{else}}
		<form method=POST>
			<div id="layout" style="display: grid; grid-template-rows: repeat(10, 1fr); grid-template-columns: repeat(8, 1fr); grid-gap: 1%; width: 50vw; height: 100vh; margin: 0 auto; padding: 1%; background-color: green; grid-template-areas: 
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
	'content content content content content content content content'
			         '. . . footer footer . image image'">
				<div style="grid-area: content">
					<textarea style="width: 99%; height: 98%; max-width: 99%; max-height: 99%" name=content></textarea>
				</div>
				<div style="grid-area: footer"></div>
				<input type=submit>
				<img src="assets/graphics/Frog_1.svg" style="grid-row: -3/span 2; grid-column: -3/span 2">
			</div>
		</form>
		{{end}}
	</body>
</html>

The lesson.html code is identical to the page_template_1_form.html code, and I’m running the whole thing on localhost. There are no errors shown when I run the code, yet it still doesn’t seem to work. Is there something I’m missing?

You’re ignoring some errors like from os.Create("lesson.html"), the os.Open on the next line and then the io.Copy after that, then the tmpl.Execute, etc. Try logging those errors to see if something’s wrong there.

At first sight you missed to declare the template in lesson.html file (eg. {{ define "lesson" }} ...template content.... {{end}} ). Also I can’t figured out why you create and load runtime the other template, however you can simply use ParseGlob to read multiple templates in one step and that can be useful especially for nested templates.