From c335c0fee92a658b6a09453ae73e5c542f10703a Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Tue, 25 Nov 2014 19:24:57 +0100 Subject: [PATCH] Fixed #23914 -- Improved {% now %} to allow storing its result in the context. Thanks to Tim for the review. --- django/template/defaulttags.py | 17 ++++++++++++++--- docs/ref/templates/builtins.txt | 12 ++++++++++++ docs/releases/1.8.txt | 3 +++ tests/template_tests/tests.py | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 8a2fb944f2..51ddfc9819 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -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): @@ -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 diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 2b3d48d90e..146f2eacd6 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -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 diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 73ad244cfb..b342234ef9 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -419,6 +419,9 @@ Templates * Added a :class:`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 ^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 71da1ee40a..67ac08601b 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -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