You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are also two additional methods for using layouts (explained below).
26
28
27
29
The Render method takes a string and a data source, which is generally a map or struct, and returns the output string. If the template file contains an error, the return value is a description of the error. There's a similar method, RenderFile, which takes a filename as an argument and uses that for the template contents.
28
30
29
-
data := mustache.Render("hello {{c}}", map[string]string{"c":"world"})
30
-
println(data)
31
-
31
+
```go
32
+
data := mustache.Render("hello {{c}}", map[string]string{"c":"world"})
33
+
println(data)
34
+
```
32
35
33
36
If you're planning to render the same template multiple times, you do it efficiently by compiling the template first:
For more example usage, please see `mustache_test.go`
42
47
@@ -48,56 +53,68 @@ mustache.go follows the official mustache HTML escaping rules. That is, if you e
48
53
49
54
It is a common pattern to include a template file as a "wrapper" for other templates. The wrapper may include a header and a footer, for instance. Mustache.go supports this pattern with the following two methods:
The layout file must have a variable called `{{content}}`. For example, given the following files:
56
63
57
64
layout.html.mustache:
58
65
59
-
<html>
60
-
<head><title>Hi</title></head>
61
-
<body>
62
-
{{{content}}}
63
-
</body>
64
-
</html>
66
+
```html
67
+
<html>
68
+
<head><title>Hi</title></head>
69
+
<body>
70
+
{{{content}}}
71
+
</body>
72
+
</html>
73
+
```
65
74
66
75
template.html.mustache:
67
76
68
-
<h1> Hello World! </h1>
77
+
```html
78
+
<h1> Hello World! </h1>
79
+
```
69
80
70
81
A call to `RenderFileInLayout("template.html.mustache", "layout.html.mustache", nil)` will produce:
71
82
72
-
<html>
73
-
<head><title>Hi</title></head>
74
-
<body>
75
-
<h1> Hello World! </h1>
76
-
</body>
77
-
</html>
83
+
```html
84
+
<html>
85
+
<head><title>Hi</title></head>
86
+
<body>
87
+
<h1> Hello World! </h1>
88
+
</body>
89
+
</html>
90
+
```
78
91
79
92
## A note about method receivers
80
93
81
94
Mustache.go supports calling methods on objects, but you have to be aware of Go's limitations. For example, lets's say you have the following type:
82
95
83
-
type Person struct {
84
-
FirstName string
85
-
LastName string
86
-
}
96
+
```go
97
+
type Person struct {
98
+
FirstName string
99
+
LastName string
100
+
}
87
101
88
-
func (p *Person) Name1() string {
89
-
return p.FirstName + " " + p.LastName
90
-
}
102
+
func (p *Person) Name1() string {
103
+
return p.FirstName + "" + p.LastName
104
+
}
91
105
92
-
func (p Person) Name2() string {
93
-
return p.FirstName + " " + p.LastName
94
-
}
106
+
func (p Person) Name2() string {
107
+
return p.FirstName + "" + p.LastName
108
+
}
109
+
```
95
110
96
111
While they appear to be identical methods, `Name1` has a pointer receiver, and `Name2` has a value receiver. Objects of type `Person`(non-pointer) can only access `Name2`, while objects of type `*Person`(person) can access both. This is by design in the Go language.
0 commit comments