mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-21 03:33:02 +00:00
tpl: Add a querify function to generate query strings inside templates
The query function will take a set of parameters specified like a dict and return a url.Values object which can be .Encode'd into a query string. Example: <a href="http://www.google.com?{{ (querify "q" "test" "page" 3).Encode | safeHTML }}">Search</a> Returns: <a href="http://www.google.com?page=3&q=test">Search</a> Closes #2257
This commit is contained in:
parent
770df77b22
commit
fbf48824ae
2 changed files with 19 additions and 0 deletions
|
@ -27,6 +27,7 @@ import (
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -1745,6 +1746,21 @@ func sha1(in interface{}) (string, error) {
|
||||||
return hex.EncodeToString(hash[:]), nil
|
return hex.EncodeToString(hash[:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// querify converts the given parameters into a url.Values object
|
||||||
|
func querify(params ...interface{}) (url.Values, error) {
|
||||||
|
qs := url.Values{}
|
||||||
|
vals, err := dictionary(params...)
|
||||||
|
if err != nil {
|
||||||
|
return url.Values{}, fmt.Errorf("querify keys must be strings")
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, value := range vals {
|
||||||
|
qs.Add(name, url.QueryEscape(fmt.Sprintf("%v", value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return qs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
funcMap = template.FuncMap{
|
funcMap = template.FuncMap{
|
||||||
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
|
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
|
||||||
|
@ -1794,6 +1810,7 @@ func init() {
|
||||||
"partial": partial,
|
"partial": partial,
|
||||||
"plainify": plainify,
|
"plainify": plainify,
|
||||||
"pluralize": pluralize,
|
"pluralize": pluralize,
|
||||||
|
"querify": querify,
|
||||||
"readDir": readDirFromWorkingDir,
|
"readDir": readDirFromWorkingDir,
|
||||||
"readFile": readFileFromWorkingDir,
|
"readFile": readFileFromWorkingDir,
|
||||||
"ref": ref,
|
"ref": ref,
|
||||||
|
|
|
@ -103,6 +103,7 @@ modBool: {{modBool 15 3}}
|
||||||
mul: {{mul 2 3}}
|
mul: {{mul 2 3}}
|
||||||
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
|
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
|
||||||
pluralize: {{ "cat" | pluralize }}
|
pluralize: {{ "cat" | pluralize }}
|
||||||
|
querify: {{ (querify "foo" 1 "bar" 2 "baz" "with spaces").Encode | safeHTML }}
|
||||||
readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
|
readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
|
||||||
readFile: {{ readFile "README.txt" }}
|
readFile: {{ readFile "README.txt" }}
|
||||||
relURL 1: {{ "http://gohugo.io/" | relURL }}
|
relURL 1: {{ "http://gohugo.io/" | relURL }}
|
||||||
|
@ -154,6 +155,7 @@ modBool: true
|
||||||
mul: 6
|
mul: 6
|
||||||
plainify: Hello world, gophers!
|
plainify: Hello world, gophers!
|
||||||
pluralize: cats
|
pluralize: cats
|
||||||
|
querify: bar=2&baz=with%2Bspaces&foo=1
|
||||||
readDir: README.txt
|
readDir: README.txt
|
||||||
readFile: Hugo Rocks!
|
readFile: Hugo Rocks!
|
||||||
relURL 1: http://gohugo.io/
|
relURL 1: http://gohugo.io/
|
||||||
|
|
Loading…
Reference in a new issue