Ticket #208: 208.3.patch

File 208.3.patch, 10.4 KB (added by EspenG, 18 years ago)

This patch is also got backwards compatibility.

  • django/contrib/admin/templates/admin/change_list_results.html

     
    1010</thead>
    1111<tbody>
    1212{% for result in results %}
    13 <tr class="{% cycle row1,row2 %}">{% for item in result %}{{ item }}{% endfor %}</tr>
     13<tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr>
    1414{% endfor %}
    1515</tbody>
    1616</table>
  • django/template/defaulttags.py

     
    22
    33from django.template import Node, NodeList, Template, Context, resolve_variable
    44from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
    5 from django.template import get_library, Library, InvalidTemplateLibrary
     5from django.template import get_library, Library, InvalidTemplateLibrary, resolve_variable
    66from django.conf import settings
    77import sys
    88
     
    2222    def render(self, context):
    2323        self.counter += 1
    2424        value = self.cyclevars[self.counter % self.cyclevars_len]
     25       value = resolve_variable(value, context)
    2526        if self.variable_name:
    2627            context[self.variable_name] = value
    2728        return value
     
    389390    the loop::
    390391
    391392        {% for o in some_list %}
    392             <tr class="{% cycle row1,row2 %}">
     393            <tr class="{% cycle 'row1' 'row2' %}">
    393394                ...
    394395            </tr>
    395396        {% endfor %}
     
    397398    Outside of a loop, give the values a unique name the first time you call
    398399    it, then use that name each sucessive time through::
    399400
    400             <tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr>
     401            <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr>
    401402            <tr class="{% cycle rowcolors %}">...</tr>
    402403            <tr class="{% cycle rowcolors %}">...</tr>
    403404
    404     You can use any number of values, seperated by commas. Make sure not to
    405     put spaces between the values -- only commas.
     405    You can use any number of values, seperated by spaces.
    406406    """
    407407
    408408    # Note: This returns the exact same node on each {% cycle name %} call; that
    409     # is, the node object returned from {% cycle a,b,c as name %} and the one
     409    # is, the node object returned from {% cycle a b c as name %} and the one
    410410    # returned from {% cycle name %} are the exact same object.  This shouldn't
    411411    # cause problems (heh), but if it does, now you know.
    412412    #
     
    415415    # a global variable, which would make cycle names have to be unique across
    416416    # *all* templates.
    417417
    418     args = token.contents.split()
     418    args = token.split_contents()
     419   
    419420    if len(args) < 2:
    420         raise TemplateSyntaxError("'Cycle' statement requires at least two arguments")
     421       raise TemplateSyntaxError("Cycle' statement requires at least two arguments")
    421422
    422     elif len(args) == 2 and "," in args[1]:
    423         # {% cycle a,b,c %}
    424         cyclevars = [v for v in args[1].split(",") if v]    # split and kill blanks
    425         return CycleNode(cyclevars)
    426         # {% cycle name %}
     423    if len(args) == 2:
     424       # {% cycle foo,bar,zoo %} case ?
     425        cyclevars = args[1].split(",")
     426        if len(cyclevars) > 1:
     427            # we need to add the extra quotation marks around the values
     428            cyclevars = ['"%s"' % x for x in cyclevars]
     429            return CycleNode(cyclevars)
    427430
    428     elif len(args) == 2:
    429         name = args[1]
    430         if not hasattr(parser, '_namedCycleNodes'):
    431             raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name)
    432         if not parser._namedCycleNodes.has_key(name):
    433             raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
    434         return parser._namedCycleNodes[name]
     431        else:
     432            # {% cycle foo %} case
     433            name = args[1]
     434            if not hasattr(parser, '_namedCycleNodes'):
     435                raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name)
     436            if not parser._namedCycleNodes.has_key(name):
     437                raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
     438            return parser._namedCycleNodes[name]
    435439
    436     elif len(args) == 4:
    437         # {% cycle a,b,c as name %}
    438         if args[2] != 'as':
    439             raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
    440         cyclevars = [v for v in args[1].split(",") if v]    # split and kill blanks
     440    if len(args) == 4 and args[2] == "as" and len(args[1].split(",")) > 1:
     441        # {% cycle a,b,c as foo %} case
     442        cyclevars = ['"%s"' % x for x in args[1].split(",")]
    441443        name = args[3]
    442444        node = CycleNode(cyclevars, name)
    443 
    444445        if not hasattr(parser, '_namedCycleNodes'):
    445446            parser._namedCycleNodes = {}
    446 
    447447        parser._namedCycleNodes[name] = node
    448448        return node
    449449
    450     else:
    451         raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args)
     450    if len(args) > 2 and args[-2]:
     451       if args[-2] == 'as':
     452           # {% cycle 'value 1' 'value 2' 'value 3' as foo %} case
     453           name = args[-1]
     454           cyclevars = args[1:-2]
     455            print name, cyclevars
     456           node = CycleNode(cyclevars, name)
     457           if not hasattr(parser, '_namedCycleNodes'):
     458               parser._namedCycleNodes = {}
     459           parser._namedCycleNodes[name] = node
     460       else:
     461           # {% cycle 'value 1' 'value 2' 'value 3' %} case
     462           cyclevars = args[1:]
     463           node = CycleNode(cyclevars)
     464       
     465        return node
     466
     467    # none of the cases were met
     468    raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args)
     469   
    452470cycle = register.tag(cycle)
    453471
    454472def debug(parser, token):
  • tests/regressiontests/templates/tests.py

     
    233233
    234234            ### CYCLE TAG #############################################################
    235235            'cycle01': ('{% cycle a %}', {}, template.TemplateSyntaxError),
    236             'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'),
    237             'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'),
    238             'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'),
    239             'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
    240             'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
    241             'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
    242             'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'),
     236            'cycle02': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}", {}, 'ab'),
     237            'cycle03': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}", {}, 'abc'),
     238            'cycle04': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}", {}, 'abca'),
     239            'cycle05': ("{% cycle %}", {}, template.TemplateSyntaxError),
     240            'cycle06': ("{% cycle a %}", {}, template.TemplateSyntaxError),
     241            'cycle07': ("{% cycle 'a' 'b' 'c' as foo %}{% cycle bar %}", {}, template.TemplateSyntaxError),
     242            'cycle08': ("{% cycle 'a' 'b' 'c' as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}",
     243                       {}, 'abbbcc'),
     244           'cycle09': ("{% cycle 'chris 1' 'camri is cute' 'cotton' as foo %}{% cycle foo %}", {}, 'chris 1camri is cute'),
     245           'cycle10': ("{% for i in test %}{% cycle 'row1' 'row2' %}{% endfor %}", {'test':[1,2]}, 'row1row2'),
     246           'cycle11': ("{% cycle one two as foo %}{% cycle foo %}", {'one':'1','two':'2'}, '12'),
     247            'cycle12': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'),
     248            'cycle13': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'),
     249            'cycle14': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'),
     250            'cycle15': ('{% cycle %}', {}, template.TemplateSyntaxError),
     251            'cycle16': ('{% cycle a %}', {}, template.TemplateSyntaxError),
     252            'cycle17': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
     253            'cycle18': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'),
    243254
    244255            ### EXCEPTIONS ############################################################
    245256
  • docs/templates.txt

     
    362362Within a loop, cycles among the given strings each time through the loop::
    363363
    364364    {% for o in some_list %}
     365        <tr class="{% cycle 'row1' 'row2' %}">
     366            ...
     367        </tr>
     368    {% endfor %}
     369 
     370Or like this::
     371
     372    {% for o in some_list %}
    365373        <tr class="{% cycle row1,row2 %}">
    366374            ...
    367375        </tr>
    368376    {% endfor %}
    369377
     378Both codes does the same job, but when you are using the comma seperated list you
     379can not use blank spaces in the values.
     380
    370381Outside of a loop, give the values a unique name the first time you call it,
    371382then use that name each successive time through::
    372383
     384        <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr>
     385        <tr class="{% cycle rowcolors %}">...</tr>
     386        <tr class="{% cycle rowcolors %}">...</tr>
     387
     388You can use any number of values,  separated by spaces.
     389
     390The example over can also be written like this::
     391
    373392        <tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr>
    374393        <tr class="{% cycle rowcolors %}">...</tr>
    375394        <tr class="{% cycle rowcolors %}">...</tr>
    376395
    377 You can use any number of values, separated by commas. Make sure not to put
    378 spaces between the values -- only commas.
     396Remember that when using the comma seperated list you can not use blank spaces.
    379397
    380398debug
    381399~~~~~
Back to Top