naturaltime- and intcomma-filters added.

This commit is contained in:
Florian Schlachter 2014-07-28 16:21:38 +02:00
parent 9fc75259ea
commit 5c75b2918a
3 changed files with 17 additions and 1 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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")
}