diff --git a/README.md b/README.md index 3e51e02..376b83c 100644 --- a/README.md +++ b/README.md @@ -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) - **[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) + - **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string) ## Tags diff --git a/filters.go b/filters.go index d808fe3..c4e80d2 100644 --- a/filters.go +++ b/filters.go @@ -19,6 +19,7 @@ func init() { pongo2.RegisterFilter("timesince", filterTimeuntilTimesince) pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince) pongo2.RegisterFilter("intcomma", filterIntcomma) + pongo2.RegisterFilter("ordinal", filterOrdinal) } 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) { 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 +} diff --git a/filters_test.go b/filters_test.go index 96f89e3..39428ef 100644 --- a/filters_test.go +++ b/filters_test.go @@ -52,4 +52,8 @@ func (s *TestSuite1) TestFilters(c *C) { // Intcomma 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") }