Filter random
added.
Random returns a random item out of the input slice.
This commit is contained in:
parent
ada12dc6c9
commit
4e07dfc9bb
20
filters.go
20
filters.go
@ -3,6 +3,7 @@ package pongo2addons
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -22,6 +23,7 @@ func init() {
|
|||||||
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
|
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
|
||||||
pongo2.RegisterFilter("truncatesentences", filterTruncatesentences)
|
pongo2.RegisterFilter("truncatesentences", filterTruncatesentences)
|
||||||
pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML)
|
pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML)
|
||||||
|
pongo2.RegisterFilter("random", filterRandom)
|
||||||
|
|
||||||
// Markup
|
// Markup
|
||||||
pongo2.RegisterFilter("markdown", filterMarkdown)
|
pongo2.RegisterFilter("markdown", filterMarkdown)
|
||||||
@ -218,6 +220,24 @@ func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2
|
|||||||
return pongo2.AsSafeValue(newOutput.String()), nil
|
return pongo2.AsSafeValue(newOutput.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterRandom(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||||
|
if !in.CanSlice() {
|
||||||
|
return nil, &pongo2.Error {
|
||||||
|
Sender: "filter:random",
|
||||||
|
OrigError: error.New("input is not sliceable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if in.Len() <= 0 {
|
||||||
|
return nil, &pongo2.Error {
|
||||||
|
Sender: "filter:random",
|
||||||
|
OrigError: error.New("input slice is empty"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return in.Index(rand.Intn(in.Len())), nil
|
||||||
|
}
|
||||||
|
|
||||||
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||||
basetime, isTime := in.Interface().(time.Time)
|
basetime, isTime := in.Interface().(time.Time)
|
||||||
if !isTime {
|
if !isTime {
|
||||||
|
Loading…
Reference in New Issue
Block a user