Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add urlParse function (i.e. url.Parse) #132

Merged
merged 2 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Gomplate is an alternative that will let you process templates which also includ
- [Example](#example)
- [`trim`](#trim)
- [Example](#example)
- [`urlParse`](#urlparse)
- [Example](#example)
- [`has`](#has)
- [Example](#example)
- [`json`](#json)
Expand Down Expand Up @@ -433,6 +435,27 @@ $ FOO=" world " | gomplate < input.tmpl
Hello, world!
```

#### `urlParse`

Parses a string as a URL for later use. Equivalent to [url.Parse](https://golang.org/pkg/net/url/#Parse)

##### Example

_`input.tmpl`:_
```
{{ $u := urlParse "https://example.com:443/foo/bar" }}
The scheme is {{ $u.Scheme }}
The host is {{ $u.Host }}
The path is {{ $u.Path }}
```

```console
$ gomplate < input.tmpl
The scheme is https
The host is example.com:443
The path is /foo/bar
```

#### `has`

Has reports whether or not a given object has a property with the given key. Can be used with `if` to prevent the template from trying to access a non-existent property in an object.
Expand Down
2 changes: 2 additions & 0 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io"
"log"
"net/url"

"strings"
"text/template"
Expand Down Expand Up @@ -69,6 +70,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
"toLower": strings.ToLower,
"trim": strings.Trim,
"trimSpace": strings.TrimSpace,
"urlParse": url.Parse,
"datasource": data.Datasource,
"ds": data.Datasource,
"datasourceExists": data.DatasourceExists,
Expand Down