ordinal-filter added.

This commit is contained in:
Florian Schlachter 2014-07-28 16:24:44 +02:00
parent 5c75b2918a
commit f8f8725dc5
3 changed files with 10 additions and 0 deletions

View File

@ -22,6 +22,7 @@ All additional filters/tags will be registered automatically.
- **[filesizeformat](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input) - **[filesizeformat](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input)
- **[timesince](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timesince)/[timeuntil](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timeuntil)/[naturaltime](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime)** (human-readable time [duration] indicator) - **[timesince](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timesince)/[timeuntil](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timeuntil)/[naturaltime](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime)** (human-readable time [duration] indicator)
- **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number) - **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number)
- **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string)
## Tags ## Tags

View File

@ -19,6 +19,7 @@ func init() {
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince) pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince) pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince)
pongo2.RegisterFilter("intcomma", filterIntcomma) pongo2.RegisterFilter("intcomma", filterIntcomma)
pongo2.RegisterFilter("ordinal", filterOrdinal)
} }
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) { func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
@ -54,3 +55,7 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va
func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) { func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil
} }
func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
return pongo2.AsValue(humanize.Ordinal(in.Integer())), nil
}

View File

@ -52,4 +52,8 @@ func (s *TestSuite1) TestFilters(c *C) {
// Intcomma // Intcomma
c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
// Ordinal
c.Assert(pongo2.RenderTemplateString("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil),
Equals, "1st 2nd 3rd 18241st")
} }