Skip to content

Commit 6375acf

Browse files
authored
Merge pull request #56 from gagliardetto/patch-1
Add code highlighting
2 parents af524a1 + f1c98d6 commit 6375acf

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

Readme.md

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,34 @@ To install mustache.go, simply run `go get github.com/hoisie/mustache`. To use i
1414
## Usage
1515
There are four main methods in this package:
1616

17-
func Render(data string, context ...interface{}) string
18-
19-
func RenderFile(filename string, context ...interface{}) string
20-
21-
func ParseString(data string) (*Template, os.Error)
22-
23-
func ParseFile(filename string) (*Template, os.Error)
17+
```go
18+
func Render(data string, context ...interface{}) string
19+
20+
func RenderFile(filename string, context ...interface{}) string
21+
22+
func ParseString(data string) (*Template, os.Error)
23+
24+
func ParseFile(filename string) (*Template, os.Error)
25+
```
2426

2527
There are also two additional methods for using layouts (explained below).
2628

2729
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.
2830

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+
```
3235
3336
If you're planning to render the same template multiple times, you do it efficiently by compiling the template first:
3437
35-
tmpl,_ := mustache.ParseString("hello {{c}}")
36-
var buf bytes.Buffer;
37-
for i := 0; i < 10; i++ {
38-
tmpl.Render (map[string]string { "c":"world"}, &buf)
39-
}
38+
```go
39+
tmpl,_ := mustache.ParseString("hello {{c}}")
40+
var buf bytes.Buffer;
41+
for i := 0; i < 10; i++ {
42+
tmpl.Render (map[string]string { "c":"world"}, &buf)
43+
}
44+
```
4045
4146
For more example usage, please see `mustache_test.go`
4247
@@ -48,56 +53,68 @@ mustache.go follows the official mustache HTML escaping rules. That is, if you e
4853
4954
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:
5055
51-
func RenderInLayout(data string, layout string, context ...interface{}) string
52-
53-
func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) string
56+
```go
57+
func RenderInLayout(data string, layout string, context ...interface{}) string
58+
59+
func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) string
60+
```
5461
5562
The layout file must have a variable called `{{content}}`. For example, given the following files:
5663
5764
layout.html.mustache:
5865
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+
```
6574
6675
template.html.mustache:
6776
68-
<h1> Hello World! </h1>
77+
```html
78+
<h1> Hello World! </h1>
79+
```
6980
7081
A call to `RenderFileInLayout("template.html.mustache", "layout.html.mustache", nil)` will produce:
7182
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+
```
7891
7992
## A note about method receivers
8093
8194
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:
8295
83-
type Person struct {
84-
FirstName string
85-
LastName string
86-
}
96+
```go
97+
type Person struct {
98+
FirstName string
99+
LastName string
100+
}
87101
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+
}
91105
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+
```
95110
96111
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.
97112
98113
So if you write the following:
99114
100-
mustache.Render("{{Name1}}", Person{"John", "Smith"})
115+
```go
116+
mustache.Render("{{Name1}}", Person{"John", "Smith"})
117+
```
101118
102119
It'll be blank. You either have to use `&Person{"John", "Smith"}`, or call `Name2`
103120
@@ -108,5 +125,3 @@ It'll be blank. You either have to use `&Person{"John", "Smith"}`, or call `Name
108125
* Change delimiter
109126
* Sections (boolean, enumerable, and inverted)
110127
* Partials
111-
112-

0 commit comments

Comments
 (0)