Views and templates

Gozen makes use of golang's internal template engine. Whilst it isn't as featured as some other third party options, we feel it is good enough. And most importantly reduces any unnecessary dependency chains.


To create a view or html file simply go to the views directory and add it there.


An important caveat to keep in mind is you can NOT create sub directories. This is quite common in other popular frameworks like Laravel or Ruby on Rails. Go is different, so all your html files must reside here and obviously each must be unique.

Did you know?

You can NOT create sub directories for your view/template files.

Extending your templates

It is quite a common practice to create a master header and footer file, which generally extends all your other templates. This is useful for not repeating yourself. In Gozen we can accomplish this by creating two files, _header.html and _footer.html.


Then all subsequent files you create can inherit these properties.


_header.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Gozen</title>
      <style>
        .text-danger{
           color:red; 
        }
    </style>
   </head>
   <body>
      <div>

_footer.html

</div>
</body>

</html>

other.html

{{ template "_header.html" }}
some content
{{ template "_footer.html" }}

Passing data to your views

func Test(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{} {
           "Foo": "testing",
        }
	// Render the template and write it to the response
	templates.Render(w, r, "forms", data)
}

To access the data 'Foo' in the above example you can add this in your html

{{ .Data.Foo }}