Files
the_information_nexus/tech_docs/go_templates.md
2024-06-01 06:15:49 +00:00

234 lines
4.2 KiB
Markdown

Sure! Let's break down the concept of actions in Go templates, represented by `{{ ... }}`, with examples for each type: variables, function calls, loops, conditionals, and more.
### 1. Variables
In Go templates, variables are placeholders that get replaced with actual data when the template is rendered. The data is typically passed to the template from the Go code.
#### Example:
```go
{{ .Title }}
```
Here, `{{ .Title }}` will be replaced by the value of the `Title` field in the data passed to the template.
#### Go Code:
```go
data := struct {
Title string
}{
Title: "Welcome to My Website",
}
tmpl.Execute(w, data)
```
#### Template:
```html
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{ .Title }}</h1>
</body>
</html>
```
### 2. Function Calls
Go templates allow you to call functions to process data before rendering it. Built-in functions can be used directly in the template.
#### Example:
```go
{{ .Date | dateFormat "2006-01-02" }}
```
Here, `{{ .Date | dateFormat "2006-01-02" }}` formats the date using the `dateFormat` function.
#### Go Code:
```go
data := struct {
Date time.Time
}{
Date: time.Now(),
}
tmpl.Execute(w, data)
```
#### Template:
```html
<!DOCTYPE html>
<html>
<head>
<title>Date Example</title>
</head>
<body>
<p>{{ .Date | dateFormat "2006-01-02" }}</p>
</body>
</html>
```
### 3. Loops
Loops allow you to iterate over collections like arrays or slices.
#### Example:
```go
{{ range .Items }}
<li>{{ . }}</li>
{{ end }}
```
Here, `{{ range .Items }}` loops through each item in the `Items` collection.
#### Go Code:
```go
data := struct {
Items []string
}{
Items: []string{"Item 1", "Item 2", "Item 3"},
}
tmpl.Execute(w, data)
```
#### Template:
```html
<!DOCTYPE html>
<html>
<head>
<title>Loop Example</title>
</head>
<body>
<ul>
{{ range .Items }}
<li>{{ . }}</li>
{{ end }}
</ul>
</body>
</html>
```
### 4. Conditionals
Conditionals are used to include or exclude parts of the template based on certain conditions.
#### Example:
```go
{{ if .IsAdmin }}
<p>Welcome, admin!</p>
{{ else }}
<p>Welcome, user!</p>
{{ end }}
```
Here, `{{ if .IsAdmin }}` checks if the `IsAdmin` variable is true.
#### Go Code:
```go
data := struct {
IsAdmin bool
}{
IsAdmin: true,
}
tmpl.Execute(w, data)
```
#### Template:
```html
<!DOCTYPE html>
<html>
<head>
<title>Conditional Example</title>
</head>
<body>
{{ if .IsAdmin }}
<p>Welcome, admin!</p>
{{ else }}
<p>Welcome, user!</p>
{{ end }}
</body>
</html>
```
### 5. Pipelines
Pipelines allow you to chain multiple operations together. Each operation's output becomes the input for the next operation.
#### Example:
```go
{{ .Price | multiply 1.1 | printf "%.2f" }}
```
Here, `{{ .Price | multiply 1.1 | printf "%.2f" }}` multiplies `Price` by `1.1` and then formats it to two decimal places.
#### Go Code:
```go
data := struct {
Price float64
}{
Price: 100.0,
}
tmpl.Execute(w, data)
```
#### Template:
```html
<!DOCTYPE html>
<html>
<head>
<title>Pipeline Example</title>
</head>
<body>
<p>Price: ${{ .Price | multiply 1.1 | printf "%.2f" }}</p>
</body>
</html>
```
### 6. Custom Functions
You can define custom functions in Go and pass them to the template.
#### Example:
Define a custom function to multiply a number:
```go
func multiply(a, b float64) float64 {
return a * b
}
```
#### Go Code:
```go
func multiply(a, b float64) float64 {
return a * b
}
func main() {
tmpl := template.New("example").Funcs(template.FuncMap{
"multiply": multiply,
})
tmpl, _ = tmpl.Parse(`
<!DOCTYPE html>
<html>
<head>
<title>Custom Function Example</title>
</head>
<body>
<p>Price: ${{ .Price | multiply 1.1 }}</p>
</body>
</html>`)
data := struct {
Price float64
}{
Price: 100.0,
}
tmpl.Execute(os.Stdout, data)
}
```
### Conclusion
Go templates use `{{ ... }}` to perform various actions, including accessing variables, calling functions, iterating over collections, and evaluating conditions. This flexibility allows you to create dynamic, data-driven content easily. Understanding these actions helps you utilize the full power of Go templates effectively.