Start documentation for template actions

Matt Holt 2019-07-19 19:07:03 -06:00
parent b59e0b7b89
commit 7cbbacdea2

102
v2:-Templates.md Normal file

@ -0,0 +1,102 @@
Caddy 2 features an improved [templates middleware](https://github.com/caddyserver/caddy/wiki/v2:-Documentation#httphandlerstemplates).
### `Sprig Functions`
In addition to the functions described on this page, **[all Sprig functions](http://masterminds.github.io/sprig/)** are supported as well.
### `.Args`
Access arguments passed to this page/context, for example as the result of a `.Include`.
```
{{.Args 0}} // first argument
```
### `.Cookie`
Gets the value of a cookie by name.
```
{{.Cookie "cookiename"}}
```
### `.Host`
Returns the hostname portion (no port) of the Host header of the HTTP request.
```
{{.Host}}
```
### `.HTTPInclude`
Includes the contents of another file by making a virtual HTTP request (also known as a sub-request). The URI path must exist on the same virtual server because the request does not use sockets; instead, the request is crafted in memory and the handler is invoked directly for increased efficiency.
```
{{.HTTPInclude "/foo/bar?q=val"}}
```
### `.Include`
Includes the contents of another file. Optionally can pass key-value pairs as arguments to be accessed by the included file.
```
{{.Include "path/to/file.html"}} // no arguments
{{.Include "path/to/file.html" "arg1" 2 "value 3"}} // with arguments
```
### `.ListFiles`
Returns a list of the files in the given directory, which is relative to the template context's file root.
```
{{.ListFiles "/mydir"}}
```
### `.Markdown`
Renders the given Markdown text as HTML.
```
{{.Markdown "My _markdown_ text"}}
```
### `.RemoteIP`
Returns the client's IP address.
```
{{.RemoteIP}}
```
### `.RespHeader.Add`
Adds a header field to the HTTP response.
```
{{.RespHeader.Add "Field-Name" "val"}}
```
### `.RespHeader.Del`
Deletes a header field on the HTTP response.
```
{{.RespHeader.Del "Field-Name"}}
```
### `.RespHeader.Set`
Sets a header field on the HTTP response, replacing any existing value.
```
{{.RespHeader.Set "Field-Name" "val"}}
```
### `.StripHTML`
Removes HTML from a string.
```
{{.StripHTML "Shows <b>only</b> text content"}}
```