Skip to content

Commit

Permalink
Renaming: $row() -> $cycle()
Browse files Browse the repository at this point in the history
git-svn-id: http://pony.tigris.org/svn/pony/trunk@142 698dff77-f410-0410-8918-cf622e150f36
  • Loading branch information
kozlovsky committed Apr 21, 2007
1 parent 0392f82 commit 5684b4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
16 changes: 8 additions & 8 deletions docs/template-syntax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ $else { text }

$for(var in expr) {
text
$row{text1}{text2}{text3}
$row(i,j){text}
$row("odd"){text} $// $row(1,2){text}
$row("even"){text} $// $row(2,2){text}
$row("first"){text}
$row("not first"){text}
$row("last"){text}
$row("not last"){text}
$cycle{text1}{text2}{text3}
$cycle(i,j){text}
$cycle("odd"){text} $// $cycle(1,2){text}
$cycle("even"){text} $// $cycle(2,2){text}
$cycle("first"){text}
$cycle("not first"){text}
$cycle("last"){text}
$cycle("not last"){text}
} $separator {
text
} $else {
Expand Down
29 changes: 15 additions & 14 deletions pony/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,26 +594,27 @@ def __init__(self, markup, globals, locals=None):
def __call__(self):
return self.markup.eval(self.globals, self.locals)

def _row_check(args):
def _cycle_check(args):
for arg in args:
if not isinstance(arg, BoundMarkup):
raise TypeError('Incorrect type of argument: %s' % str(type(arg)))
locals = args[-1].locals
if locals is None or 'for' not in locals: raise TypeError(
'$row() function may only be called inside a $for loop')
'$cycle() function may only be called inside a $for loop')
return locals['for']

@lazy
def row(*args):
def cycle(*args):
if len(args) < 2: raise TypeError(
'$row() function takes at least 2 arguments (%d given)' % len(args))
'$cycle() function takes at least 2 arguments (%d given)' % len(args))
if isinstance(args[0], BoundMarkup):
current, total = _row_check(args[1:])
current, total = _cycle_check(args[1:])
return args[current % len(args)]()
elif isinstance(args[0], basestring):
if len(args) != 2: raise TypeError('When first argument is string, '
'$row() function takes exactly 2 arguments (%d given)' % len(args))
current, total = _row_check(args[1:])
if len(args) != 2: raise TypeError(
'When first argument is string, $cycle() function '
'takes exactly 2 arguments (%d given)' % len(args))
current, total = _cycle_check(args[1:])
s, markup = args
if s == 'odd': flag = not (current % 2) # rows treated as 1-based!
elif s == 'even': flag = (current % 2) # rows treated as 1-based!
Expand All @@ -623,23 +624,23 @@ def row(*args):
elif s == 'not last': flag = (current != total - 1)
return flag and markup() or ''
elif not isinstance(args[0], (int, long)):
raise TypeError('First argument of $row() function must be int, '
raise TypeError('First argument of $cycle() function must be int, '
'string or markup. Got: %s' % str(type(args[0])))
elif isinstance(args[1], (int, long)):
if len(args) == 2: raise TypeError('Markup expected')
if len(args) > 3:
raise TypeError('$row() function got too many arguments')
current, total = _row_check(args[2:])
raise TypeError('$cycle() function got too many arguments')
current, total = _cycle_check(args[2:])
i, j, markup = args
return (current % j == i - 1) and markup() or ''
else:
if len(args) > 2:
raise TypeError('$row() function got too many arguments')
current, total = _row_check(args[1:])
raise TypeError('$cycle() function got too many arguments')
current, total = _cycle_check(args[1:])
i, markup = args
if i > 0: return (current == i - 1) and markup() or ''
elif i < 0: return (i == current - total) and markup() or ''
else: raise TypeError('$row first argument cannot be 0')
else: raise TypeError('$cycle first argument cannot be 0')

codename_cache = {}

Expand Down

0 comments on commit 5684b4d

Please sign in to comment.