Browse Source

Add documentation for using layout files with templates #219

techjanitor 10 years ago
parent
commit
bee03fa7b0
1 changed files with 27 additions and 0 deletions
  1. 27 0
      README.md

+ 27 - 0
README.md

@@ -439,6 +439,33 @@ func main() {
 }
 }
 ```
 ```
 
 
+#####Using layout files with templates
+```go
+var baseTemplate = "main.tmpl"
+
+r.GET("/", func(c *gin.Context) {
+    r.SetHTMLTemplate(template.Must(template.ParseFiles(baseTemplate, "whatever.tmpl")))
+    c.HTML(200, "base", data)
+})
+```
+main.tmpl
+```html
+{{define "base"}}
+<html>
+    <head></head>
+    <body>
+        {{template "content" .}}
+    </body>
+</html>
+{{end}}
+```
+whatever.tmpl
+```html
+{{define "content"}}
+<h1>Hello World!</h1>
+{{end}}
+```
+
 #### Redirects
 #### Redirects
 
 
 Issuing a HTTP redirect is easy:
 Issuing a HTTP redirect is easy: