Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Templating and Views

26. Templating and Views

Templating and views are essential aspects of web development that allow you to separate the structure and presentation of your web pages from the underlying code. In Go, you can use the built-in 'html/template' package to create dynamic templates and render views. Let's explore these concepts in detail with examples.

Creating Dynamic Templates with "html/template":

The 'html/template' package in Go allows you to create templates that define the structure of your HTML pages along with placeholders for dynamic content.

Example Template:
package main

import (
    "html/template"
    "net/http"
)

type PageData struct {
    Title   string
    Content string
}

func main() {
    http.HandleFunc("/", dynamicHandler)
    http.ListenAndServe(":8080", nil)
}

func dynamicHandler(w http.ResponseWriter, r *http.Request) {
    data := PageData{
        Title:   "Dynamic Page",
        Content: "This is dynamically generated content using Go templates.",
    }

    tmpl, err := template.New("index").Parse(`
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <h1></h1>
        <p></p>
    </body>
    </html>
    `)

    if err != nil {
        http.Error(w, "Internal Server Error", http.StatusInternalServerError)
        return
    }

    err = tmpl.Execute(w, data)
    if err != nil {
        http.Error(w, "Internal Server Error", http.StatusInternalServerError)
    }
}
 

 

In this example, we define a 'PageData' struct to hold the dynamic content. We then create an HTML template using the 'template.New()'function and parse it using '.Parse()'. The placeholders like ''and '' are replaced with actual data using the 'Execute()' method.

Using Layouts and Partials:

Templates often use layouts and partials to maintain consistency across multiple pages.

Example Layout:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
   
</body>
</html>

 

Example Content Partial:

<h1></h1>
<p></p>

     

 

Example Page:
tmpl, err := template.New("page").ParseFiles("layout.tmpl", "content.tmpl")
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

data := PageData{
Title: "Dynamic Page",
Content: "This is dynamically generated content using Go templates.",
}

tmpl.ExecuteTemplate(w, "layout", data)

 

In this example, we define a layout template and a content partial. We then use the  'ParseFiles()' method to parse both templates and use  'ExecuteTemplate()' to render the page.

Summary:

  • The  'html/template' package allows you to create dynamic templates for web pages.
  • Templates define the structure of HTML pages and placeholders for dynamic content.
  • Layouts and partials help maintain consistency across multiple pages.
  • Data is passed to templates using structs, and placeholders are replaced using the 'Execute()' or  'ExecuteTemplate()' methods.

By utilizing templating and views, you can create dynamic and user-friendly web pages that separate presentation from logic. This separation improves code maintainability and allows for easy customization of the user interface.