Loading django/template/defaulttags.py +14 −3 Original line number Diff line number Diff line Loading @@ -419,12 +419,19 @@ class LoadNode(Node): class NowNode(Node): def __init__(self, format_string): def __init__(self, format_string, asvar=None): self.format_string = format_string self.asvar = asvar def render(self, context): tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None return date(datetime.now(tz=tzinfo), self.format_string) formatted = date(datetime.now(tz=tzinfo), self.format_string) if self.asvar: context[self.asvar] = formatted return '' else: return formatted class SpacelessNode(Node): Loading Loading @@ -1200,10 +1207,14 @@ def now(parser, token): It is {% now "jS F Y H:i" %} """ bits = token.split_contents() asvar = None if len(bits) == 4 and bits[-2] == 'as': asvar = bits[-1] bits = bits[:-2] if len(bits) != 2: raise TemplateSyntaxError("'now' statement takes one argument") format_string = bits[1][1:-1] return NowNode(format_string) return NowNode(format_string, asvar) @register.tag Loading docs/ref/templates/builtins.txt +12 −0 Original line number Diff line number Diff line Loading @@ -799,6 +799,18 @@ This would display as "It is the 4th of September". It is {% now "SHORT_DATETIME_FORMAT" %} You can also use the syntax ``{% now "Y" as current_year %}`` to store the output inside a variable. This is useful if you want to use ``{% now %}`` inside a template tag like :ttag:`blocktrans` for example:: {% now "Y" as current_year %} {% blocktrans %}Copyright {{ current_year }}{% endblocktrans %} .. versionadded:: 1.8 The ability to use the "as" syntax was added. .. templatetag:: regroup regroup Loading docs/releases/1.8.txt +3 −0 Original line number Diff line number Diff line Loading @@ -419,6 +419,9 @@ Templates * Added a :class:`locmem.Loader <django.template.loaders.locmem.Loader>` class that loads Django templates from a Python dictionary. * The :ttag:`now` tag can now store its output in a context variable with the usual syntax: ``{% now 'j n Y' as varname %}``. Requests and Responses ^^^^^^^^^^^^^^^^^^^^^^ Loading tests/template_tests/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -1697,6 +1697,8 @@ class TemplateTests(TestCase): datetime.now().day, datetime.now().month, datetime.now().year)), 'now06': ('''{% now "j 'n' Y"%}''', {}, '''%d '%d' %d''' % ( datetime.now().day, datetime.now().month, datetime.now().year)), 'now07': ('''{% now "j n Y" as N %}-{{N}}-''', {}, '''-%d %d %d-''' % ( datetime.now().day, datetime.now().month, datetime.now().year)), ### URL TAG ######################################################## # Successes Loading Loading
django/template/defaulttags.py +14 −3 Original line number Diff line number Diff line Loading @@ -419,12 +419,19 @@ class LoadNode(Node): class NowNode(Node): def __init__(self, format_string): def __init__(self, format_string, asvar=None): self.format_string = format_string self.asvar = asvar def render(self, context): tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None return date(datetime.now(tz=tzinfo), self.format_string) formatted = date(datetime.now(tz=tzinfo), self.format_string) if self.asvar: context[self.asvar] = formatted return '' else: return formatted class SpacelessNode(Node): Loading Loading @@ -1200,10 +1207,14 @@ def now(parser, token): It is {% now "jS F Y H:i" %} """ bits = token.split_contents() asvar = None if len(bits) == 4 and bits[-2] == 'as': asvar = bits[-1] bits = bits[:-2] if len(bits) != 2: raise TemplateSyntaxError("'now' statement takes one argument") format_string = bits[1][1:-1] return NowNode(format_string) return NowNode(format_string, asvar) @register.tag Loading
docs/ref/templates/builtins.txt +12 −0 Original line number Diff line number Diff line Loading @@ -799,6 +799,18 @@ This would display as "It is the 4th of September". It is {% now "SHORT_DATETIME_FORMAT" %} You can also use the syntax ``{% now "Y" as current_year %}`` to store the output inside a variable. This is useful if you want to use ``{% now %}`` inside a template tag like :ttag:`blocktrans` for example:: {% now "Y" as current_year %} {% blocktrans %}Copyright {{ current_year }}{% endblocktrans %} .. versionadded:: 1.8 The ability to use the "as" syntax was added. .. templatetag:: regroup regroup Loading
docs/releases/1.8.txt +3 −0 Original line number Diff line number Diff line Loading @@ -419,6 +419,9 @@ Templates * Added a :class:`locmem.Loader <django.template.loaders.locmem.Loader>` class that loads Django templates from a Python dictionary. * The :ttag:`now` tag can now store its output in a context variable with the usual syntax: ``{% now 'j n Y' as varname %}``. Requests and Responses ^^^^^^^^^^^^^^^^^^^^^^ Loading
tests/template_tests/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -1697,6 +1697,8 @@ class TemplateTests(TestCase): datetime.now().day, datetime.now().month, datetime.now().year)), 'now06': ('''{% now "j 'n' Y"%}''', {}, '''%d '%d' %d''' % ( datetime.now().day, datetime.now().month, datetime.now().year)), 'now07': ('''{% now "j n Y" as N %}-{{N}}-''', {}, '''-%d %d %d-''' % ( datetime.now().day, datetime.now().month, datetime.now().year)), ### URL TAG ######################################################## # Successes Loading