from django import template from datetime import datetime import re from post.models import Post register = template.Library() @register.tag(name='get_current_time') def do_current_time(parser,token): ''' A custom tag need 2 steps: ''' # This version uses a regular expression to parse tag contents try: # Splitting by None == splitting by spaces tag_name, arg = token.contents.split(None,1) except ValueError: msg = '%r tag requires arguments.' % token.contents[0] raise template.TemplateSyntaxError(msg) m = re.search(r'(.*?) as (\w+)',arg) if m: fmt,var_name = m.groups() else: msg = '%r tag had invalid arguments.' % tag_name raise template.TemplateSyntaxError(msg) if not (fmt[0] == fmt[-1] and fmt [0] in ('"',"'")): msg = "%r tag's argument should be in quotes." % tag_name raise template.TemplateSyntaxError(msg) return CurrentTimeNode(fmt[1:-1],var_name) class CurrentTimeNode(template.Node): ''' A custom tag Node. ''' def __init__(self,format_string,var_name): self.format_string = str(format_string) self.var_name = var_name def render(self,context): now = datetime.now() context[self.var_name] = now.strftime(self.format_string) return '' @register.tag(name='upper') def do_upper(parser,token): nodelist = parser.parse(('endupper',)) parser.delete_first_token() return UpperNode(nodelist) class UpperNode(template.Node): def __init__(self,nodelist): self.nodelist = nodelist def render(self,context): output = self.nodelist.render(context) return output.upper() @register.simple_tag def a_current_time(format_string): ''' Simple tag ''' try: return datetime.now().strftime(str(format_string)) except UnicodeEncodeError: return '' @register.inclusion_tag('tags/inclusion.html') def post_for_member(member): ''' Inclusion tag sample ''' post_list = Post.objects.filter(user__id=member.id) return {'post_list':post_list}