Skip to content

Commit

Permalink
Use original block order if selection is None
Browse files Browse the repository at this point in the history
  • Loading branch information
grahambell committed Oct 13, 2016
1 parent 9acdf38 commit 68f4b48
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This takes one argument, specifying the order in which to show the
contained blocks.
(Any non-block contents are ignored.)
In this case this argument is given by the `article_order` parameter.
When this is undefined, as it is here, the blocks are shown in their
When this is undefined, as it is here, (or None) the blocks are shown in their
original ordering.

However there might also be some special kinds of articles, such as
Expand Down
12 changes: 9 additions & 3 deletions lib/jinja2_orderblocks/orderblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ def parse(self, parser):

block_names.append(node.name)

# Make a "For" node iterating over the given block selection.
# If the block selection is undefined then use the original ordering.
# Make a "For" node iterating over the given block selection. If the
# block selection is undefined or None then use the original ordering.
return nodes.For(
block_name,
nodes.CondExpr(
nodes.Test(block_selection, 'defined', [], [], None, None),
nodes.And(
nodes.Test(block_selection, 'defined',
[], [], None, None),
nodes.Not(
nodes.Test(block_selection, 'none',
[], [], None, None)),
),
block_selection,
nodes.Tuple([nodes.Const(x) for x in block_names], 'store')),
blocks, [], None, False)
1 change: 1 addition & 0 deletions test/test_orderblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_basic(self):
'e')

self.assertEqual(template.render(), 'abcde')
self.assertEqual(template.render(blocks=None), 'abcde')
self.assertEqual(template.render(blocks=('x', 'y', 'z')), 'abcde')
self.assertEqual(template.render(blocks=('z', 'y', 'x')), 'adcbe')
self.assertEqual(template.render(blocks=('z', 'x', 'y')), 'adbce')
Expand Down

0 comments on commit 68f4b48

Please sign in to comment.