Search code examples
pythontemplatesloopsmako

Does python mako template support connitue/break in loop context?


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

For example:

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

Thanks,


Solution

  • 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