Skip to content

Commit

Permalink
Support files lists or tuples in Jinja2 assets block.
Browse files Browse the repository at this point in the history
E.g.: `{% assets 'f1', ('f2', 'f3'), ['f4'] %}`
renders like `{% assets 'f1', 'f2', 'f3', 'f4' %}`.

It helps assets composition, enabling something like *varargs
(not supported by Jinja2).
  • Loading branch information
iurisilvio committed Nov 25, 2013
1 parent 7d76ec6 commit 2c2851f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/webassets/ext/jinja2.py
Expand Up @@ -75,7 +75,11 @@ def parse(self, parser):
# Otherwise assume a source file is given, which may be any
# expression, except note that strings are handled separately above.
else:
files.append(parser.parse_expression())
expression = parser.parse_expression()
if isinstance(expression, (nodes.List, nodes.Tuple)):
files.extend(expression.iter_child_nodes())
else:
files.append(expression)

# Parse the contents of this tag
body = parser.parse_statements(['name:endassets'], drop_needle=True)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_ext/test_jinja2.py
Expand Up @@ -63,6 +63,18 @@ def test_reference_files(self):
self.render_template('"file1", "file2", "file3"')
assert self.the_bundle.contents == ('file1', 'file2', 'file3',)

def test_reference_files_list(self):
self.render_template('["file1", "file2", "file3"]')
assert self.the_bundle.contents == ('file1', 'file2', 'file3',)

def test_reference_files_tuple(self):
self.render_template('("file1", "file2", "file3")')
assert self.the_bundle.contents == ('file1', 'file2', 'file3',)

def test_reference_files_mixed(self):
self.render_template('"file1", ("file2", "file3")')
assert self.the_bundle.contents == ('file1', 'file2', 'file3',)

def test_reference_mixed(self):
self.render_template('"foo_bundle", "file2", "file3"')
assert self.the_bundle.contents == (self.foo_bundle, 'file2', 'file3',)
Expand Down

0 comments on commit 2c2851f

Please sign in to comment.