Skip to content

Commit 836d624

Browse files
committed
Added documentation for layout-related methods
1 parent 762c4da commit 836d624

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Readme.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Also check out some [example mustache files](http://github.com/defunkt/mustache/
1212
To install mustache.go, simply run `goinstall github.com/hoisie/mustache.go`. To use it in a program, use `import "github.com/hoisie/mustache.go"`
1313

1414
## Usage
15-
There are only four methods in this package:
15+
There are four main methods in this package:
1616

1717
func Render(data string, context ...interface{}) string
1818

@@ -22,6 +22,7 @@ There are only four methods in this package:
2222

2323
func ParseFile(filename string) (*template, os.Error)
2424

25+
There are also two additional methods for using layouts (explained below).
2526

2627
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.
2728

@@ -42,7 +43,39 @@ For more example usage, please see `mustache_test.go`
4243
## Escaping
4344

4445
mustache.go follows the official mustache HTML escaping rules. That is, if you enclose a variable with two curly brackets, `{{var}}`, the contents are HTML-escaped. For instance, strings like `5 > 2` are converted to `5 > 2`. To use raw characters, use three curly brackets `{{{var}}}`.
45-
46+
47+
## Layouts
48+
49+
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:
50+
51+
func RenderInLayout(data string, layout string, context ...interface{}) string
52+
53+
func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) string
54+
55+
The layout file must have a variable called `{{content}}`. For example, given the following files:
56+
57+
layout.html.mustache:
58+
59+
<html>
60+
<head><title>Hi</title></head>
61+
<body>
62+
{{{content}}}
63+
</body>
64+
</html>
65+
66+
template.html.mustache:
67+
68+
<h1> Hello World! </h1>
69+
70+
A call to `RenderFileInLayout("template.html.mustache", "layout.html.mustache", nil) will produce:
71+
72+
<html>
73+
<head><title>Hi</title></head>
74+
<body>
75+
<h1> Hello World! </h1>
76+
</body>
77+
</html>
78+
4679
## Supported features
4780

4881
* Variables

mustache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,5 +600,5 @@ func RenderFileInLayout(filename string, layoutFile string, context ...interface
600600
if err != nil {
601601
return err.String()
602602
}
603-
return tmpl.Render(context...)
603+
return tmpl.RenderInLayout(layoutTmpl, context...)
604604
}

0 commit comments

Comments
 (0)