diegoparra / web-dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

web-dev Golang

Last lesson:

Notes:

Section 4 - Creating our first template

  • When creating templates you must parse the files (html templates) and then execute the parsed files

    Example:
    	t, err := template.ParseFiles("./tmpl.html")
    	if err != nil {
    		panic(err)
    	}
    
    	u := User{
    		Name: "Diego",
    	}
    
    	err = t.Execute(os.Stdout, u)
    
  • In order to run the code above you must be at the same path, otherwise it will throw an error. To avoid this kind of problem we should embbed with the binary.

  • Exercise:

    Create a User struct containing the fields Name and Surname
    type User struct {
        Name string
        Surname string
    }
    Start parsing the html template
    t, err := template.ParseFiles("./tmpl.html")
    if err != nil {
    	panic(err)
    }
    Create a new user of type User
    u := User{
        Name: "Diego",
        Surname: "Parra",
    }
    Execute the parsed template, it should throw on the Standand Output
    err = t.Execute(os.Stdout, u)

About


Languages

Language:Go 71.9%Language:HTML 28.1%