diff --git a/README.md b/README.md index a3096be..3e51e02 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ All additional filters/tags will be registered automatically. - **markdown** (parses markdown text and outputs HTML; **hint**: use the **safe**-filter to make the output not being escaped) - **[slugify](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#slugify)** (creates a slug for a given 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)** (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) ## Tags diff --git a/filters.go b/filters.go index eae8e81..d808fe3 100644 --- a/filters.go +++ b/filters.go @@ -17,6 +17,8 @@ func init() { pongo2.RegisterFilter("filesizeformat", filterFilesizeformat) pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince) pongo2.RegisterFilter("timesince", filterTimeuntilTimesince) + pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince) + pongo2.RegisterFilter("intcomma", filterIntcomma) } func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) { @@ -48,3 +50,7 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va return pongo2.AsValue(humanize.TimeDuration(basetime.Sub(paramtime))), nil } + +func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) { + return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil +} diff --git a/filters_test.go b/filters_test.go index 39fc772..96f89e3 100644 --- a/filters_test.go +++ b/filters_test.go @@ -43,4 +43,13 @@ func (s *TestSuite1) TestFilters(c *C) { future_date = base_date.Add(2 * time.Hour) c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}", pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago") + + // Natural time + base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) + future_date = base_date.Add(4 * time.Second) + c.Assert(pongo2.RenderTemplateString("{{ base_date|naturaltime:future_date }}", + pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 seconds ago") + + // Intcomma + c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") }