mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-05 08:42:12 +00:00
Add jsonify template func
This commit is contained in:
parent
c8c6f5d05b
commit
435e996c4f
|
@ -112,6 +112,14 @@ e.g.
|
||||||
{{ .Render "summary" }}
|
{{ .Render "summary" }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|
||||||
|
### jsonify
|
||||||
|
Encodes a given object to JSON.
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
|
||||||
|
{{ dict "title" .Title "content" .Plain | jsonify }}
|
||||||
|
|
||||||
### last
|
### last
|
||||||
Slices an array to only the last _N_ elements.
|
Slices an array to only the last _N_ elements.
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ package tpl
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
|
@ -1142,6 +1143,15 @@ func markdownify(text string) template.HTML {
|
||||||
return template.HTML(m)
|
return template.HTML(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// jsonify encodes a given object to JSON.
|
||||||
|
func jsonify(v interface{}) (template.HTML, error) {
|
||||||
|
b, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return template.HTML(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
func refPage(page interface{}, ref, methodName string) template.HTML {
|
func refPage(page interface{}, ref, methodName string) template.HTML {
|
||||||
value := reflect.ValueOf(page)
|
value := reflect.ValueOf(page)
|
||||||
|
|
||||||
|
@ -1522,6 +1532,7 @@ func init() {
|
||||||
"intersect": intersect,
|
"intersect": intersect,
|
||||||
"isSet": isSet,
|
"isSet": isSet,
|
||||||
"isset": isSet,
|
"isset": isSet,
|
||||||
|
"jsonify": jsonify,
|
||||||
"last": last,
|
"last": last,
|
||||||
"le": le,
|
"le": le,
|
||||||
"lower": func(a string) string { return strings.ToLower(a) },
|
"lower": func(a string) string { return strings.ToLower(a) },
|
||||||
|
|
|
@ -98,6 +98,7 @@ in: {{ if in "this string contains a substring" "substring" }}Substring found!{{
|
||||||
seq: {{ seq 3 }}
|
seq: {{ seq 3 }}
|
||||||
sort: {{ slice "B" "C" "A" | sort }}
|
sort: {{ slice "B" "C" "A" | sort }}
|
||||||
delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
|
delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
|
||||||
|
jsonify: {{ (slice "A" "B" "C") | jsonify }}
|
||||||
`
|
`
|
||||||
expected := `chomp: <p>Blockhead</p>
|
expected := `chomp: <p>Blockhead</p>
|
||||||
dateFormat: Wednesday, Jan 21, 2015
|
dateFormat: Wednesday, Jan 21, 2015
|
||||||
|
@ -132,6 +133,7 @@ in: Substring found!
|
||||||
seq: [1 2 3]
|
seq: [1 2 3]
|
||||||
sort: [A B C]
|
sort: [A B C]
|
||||||
delimit: A, B and C
|
delimit: A, B and C
|
||||||
|
jsonify: ["A","B","C"]
|
||||||
`
|
`
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
|
Loading…
Reference in a new issue