10

Is it possible to use continue/break in a %control structure loop.

For example:

% for x in range(1):
 % continue
% endfor

Thanks,

kerwin
  • 941
  • 2
  • 12
  • 22
  • 4
    Have you tried it out yourself? – satoru Jun 16 '12 at 02:36
  • 1
    sory~ That's why i ask here. the code i post above it not working, continue should go to <% continue%>. I don't know much about mako and it's save time to ask question here. – kerwin Jun 16 '12 at 03:26

1 Answers1

18

Yes. You use <% continue %> and <% break %>.

Example:

from mako.template import Template 
t = Template( 
""" 
% for i in xrange(5): 
    % if i == 3: 
        <% break %> 
    % endif 
    ${i} 
% endfor 
% for i in xrange(5): 
    % if i == 3: 
        <% continue %> 
    % endif 
    ${i} 
% endfor 
""") 
print t.render() 

Output:

0 
1 
2 
0 
1 
2 
4 
aioobe
  • 413,195
  • 112
  • 811
  • 826
shihongzhi
  • 1,921
  • 16
  • 17
  • 3
    For those that are still too lazy to read this: Yes, it does support it. – Alyssa Haroldsen Jun 23 '15 at 20:57
  • if I'm reproducing this correctly, the continue statement cannot be embedded in an include subtemplate but must occur in the template where the for loop is defined unfortunately. – jxramos May 03 '21 at 22:40