Created
August 24, 2010 14:30
-
-
Save justquick/547645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import template | |
| register = template.Library() | |
| class FormatTimeNode(template.Node): | |
| def __init__(self, date_to_be_formatted, format_string): | |
| self.date_to_be_formatted = template.Variable(date_to_be_formatted) | |
| self.format_string = format_string | |
| def render(self, context): | |
| try: | |
| actual_date = self.date_to_be_formatted.resolve(context) | |
| return actual_date.strftime(self.format_string) | |
| except template.VariableDoesNotExist: | |
| return '' | |
| def do_format_time(parser, token): | |
| try: | |
| tag_name, date_to_be_formatted, format_string = token.contents.split() | |
| except ValueError: | |
| raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0] | |
| if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")): | |
| raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name | |
| return FormatTimeNode(date_to_be_formatted, format_string[1:-1]) | |
| register.tag('format_time', do_format_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment