Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2320 -- corrected numerous errors in the custom tag examples in

python_templates.txt. Also fixed an argument parsing error for such tags.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3308 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 927d87d73207c0fbc6200386407787c2dd205c98 1 parent c184729
Malcolm Tredinnick authored July 10, 2006
4  django/template/__init__.py
@@ -171,7 +171,7 @@ def __str__(self):
171 171
             self.contents[:20].replace('\n', ''))
172 172
 
173 173
     def split_contents(self):
174  
-        return smart_split(self.contents)
  174
+        return list(smart_split(self.contents))
175 175
 
176 176
 class Lexer(object):
177 177
     def __init__(self, template_string, origin):
@@ -758,7 +758,7 @@ def render(self, context):
758 758
 
759 759
 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
760 760
     "Returns a template.Node subclass."
761  
-    bits = token.contents.split()[1:]
  761
+    bits = token.split_contents()[1:]
762 762
     bmax = len(params)
763 763
     def_len = defaults and len(defaults) or 0
764 764
     bmin = bmax - def_len
41  docs/templates_python.txt
@@ -643,7 +643,7 @@ the current date/time, formatted according to a parameter given in the tag, in
643 643
 `strftime syntax`_. It's a good idea to decide the tag syntax before anything
644 644
 else. In our case, let's say the tag should be used like this::
645 645
 
646  
-    <p>The time is {% current_time "%Y-%M-%d %I:%M %p" %}.</p>
  646
+    <p>The time is {% current_time "%Y-%m-%d %I:%M %p" %}.</p>
647 647
 
648 648
 .. _`strftime syntax`: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
649 649
 
@@ -653,10 +653,10 @@ object::
653 653
     from django import template
654 654
     def do_current_time(parser, token):
655 655
         try:
656  
-            # Splitting by None == splitting by spaces.
657  
-            tag_name, format_string = token.contents.split(None, 1)
  656
+            # split_contents() knows not to split quoted strings.
  657
+            tag_name, format_string = token.split_contents()
658 658
         except ValueError:
659  
-            raise template.TemplateSyntaxError, "%r tag requires an argument" % token.contents[0]
  659
+            raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents[0]
660 660
         if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
661 661
             raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
662 662
         return CurrentTimeNode(format_string[1:-1])
@@ -667,7 +667,11 @@ Notes:
667 667
       example.
668 668
 
669 669
     * ``token.contents`` is a string of the raw contents of the tag. In our
670  
-      example, it's ``'current_time "%Y-%M-%d %I:%M %p"'``.
  670
+      example, it's ``'current_time "%Y-%m-%d %I:%M %p"'``.
  671
+
  672
+    * The ``token.split_contents()`` method separates the arguments on spaces,
  673
+      whilst keeping quoted strings together. The more straightforward
  674
+      ``token.contents.split()`` would be as robust.
671 675
 
672 676
     * This function is responsible for raising
673 677
       ``django.template.TemplateSyntaxError``, with helpful messages, for
@@ -681,7 +685,7 @@ Notes:
681 685
 
682 686
     * The function returns a ``CurrentTimeNode`` with everything the node needs
683 687
       to know about this tag. In this case, it just passes the argument --
684  
-      ``"%Y-%M-%d %I:%M %p"``. The leading and trailing quotes from the
  688
+      ``"%Y-%m-%d %I:%M %p"``. The leading and trailing quotes from the
685 689
       template tag are removed in ``format_string[1:-1]``.
686 690
 
687 691
     * The parsing is very low-level. The Django developers have experimented
@@ -766,27 +770,24 @@ registers it with the template system.
766 770
 
767 771
 Our earlier ``current_time`` function could thus be written like this::
768 772
 
769  
-    # This version of do_current_time takes only a single argument and returns
770  
-    # a string.
  773
+    def current_time(format_string):
  774
+        return datetime.datetime.now().strftime(format_string)
771 775
 
772  
-    def do_current_time(token):
773  
-        try:
774  
-            # Splitting by None == splitting by spaces.
775  
-            tag_name, format_string = token.contents.split(None, 1)
776  
-        except ValueError:
777  
-            raise template.TemplateSyntaxError, "%r tag requires an argument" % token.contents[0]
778  
-        if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
779  
-            raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
780  
-        return datetime.datetime.now().strftime(self.format_string[1:-1])
781  
-
782  
-    register.simple_tag(do_current_time)
  776
+    register.simple_tag(current_time)
783 777
 
784 778
 In Python 2.4, the decorator syntax also works::
785 779
 
786 780
     @register.simple_tag
787  
-    def do_current_time(token):
  781
+    def current_time(token):
788 782
         ...
789 783
 
  784
+A couple of things to note about the ``simple_tag`` helper function:
  785
+    * Only the (single) argument is passed into our function.i
  786
+    * Checking for the required number of arguments, etc, has already been
  787
+      done by the time our function is called, so we don't need to do that.
  788
+    * The quotes around the argument (if any) have already been stripped away,
  789
+      so we just receive a plain string.
  790
+
790 791
 Inclusion tags
791 792
 ~~~~~~~~~~~~~~
792 793
 

0 notes on commit 927d87d

Please sign in to comment.
Something went wrong with that request. Please try again.