Opened 8 years ago

Closed 8 years ago

#28002 closed Bug (worksforme)

named cycle tags don't work if cycle is instantiated outside included template

Reported by: kapil garg Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The following code does not work. I don't know if this is the desired behaviour or not.

def test_cycle():
        engine = Engine(loaders=[
            ('django.template.loaders.locmem.Loader', {
                'template': '{% for x in vars %}{%cycle "a" "b" "c" as cycler%}{% include "include" %}{% endfor %}',
                'include': '{% cycle cycler %}',
            }),
        ])
        output = engine.render_to_string('template', dict(vars=range(10)))
        print(output)

This is a pretty normal use case when cycle values are passed from another template.

Change History (5)

comment:1 by kapil garg, 8 years ago

Currently, cycle tags don't even work in include tag but a patch has been proposed in here.
But the named tags problem will still arise because it is handled by the parser.

comment:2 by Tim Graham, 8 years ago

Is the use case solved using the silent option? e.g.

from django.template.engine import Engine
engine = Engine(loaders=[
    ('django.template.loaders.locmem.Loader', {
        'template': '{% for x in vars %}{% cycle "a" "b" "c" as cycler silent %}{% include "include" %}{% endfor %}',
        'include': '{{ cycler }}',
    }),
])
output = engine.render_to_string('template', dict(vars=range(10)))

The output is 'abcabcabca'.

comment:3 by kapil garg, 8 years ago

If we use the cycle variable inside the included template like a normal variable then there is no problem but if we try to cycle through the variable in included template then it will raise TemplateSyntax Error.

def test_cycle():
        engine = Engine(loaders=[
            ('django.template.loaders.locmem.Loader', {
                'template': '{%cycle "a" "b" "c" as cycler silent%}{% for x in vars %}{% include "include" %}{% endfor %}',
                'include': '{% cycle cycler %}',
            }),
        ])
        output = engine.render_to_string('template', dict(vars=range(10)))
        print(output)

The above code raises TemplateSyntaxError for "include" template because for this template the "cycler" name has never been introduced before.

comment:4 by Tim Graham, 8 years ago

My question is if the expected behavior of your snippet is different from the version I proposed? I'm not sure if we should try to make your version work or if it can be made to work, but if there's already a way to accomplish the use case, then it doesn't seem necessary.

comment:5 by kapil garg, 8 years ago

Resolution: worksforme
Status: newclosed

Yes, your version serves the purpose. I did not catch that.

Note: See TracTickets for help on using tickets.
Back to Top