Skip to content
Snippets Groups Projects
Commit a5037ad2 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Templates: allow use of iterators with 'list' and 'join' filters

parent 5e983afd
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,11 @@ Config
* Added shared namelist settings ([[namelist_a, namelist_b]])
Templates
---------
* Allow use of iterators with 'list' and 'join' filters
Release 1.2.0
=============
......
......@@ -7,6 +7,7 @@
from __future__ import print_function
from collections.abc import Iterator
import io
import os
import re
......@@ -358,7 +359,7 @@ def list_singleton(eval_ctx, x, keep_empty=False, *args, **kwargs):
# Workaround for 2.8 bug when applied to literals
if isinstance(x, jinja2.nodes.EvalContext):
(eval_ctx, x) = (x, eval_ctx)
if isinstance(x, (list, tuple)):
if isinstance(x, (list, tuple, Iterator)):
if getattr(list_original, 'evalcontextfilter', getattr(list_original, 'jinja_pass_arg', False)):
return list_original(eval_ctx, x, *args, **kwargs)
return list_original(x, *args, **kwargs)
......@@ -374,7 +375,7 @@ def join_singleton(eval_ctx, x, *args, **kwargs):
# Workaround for 2.8 bug when applied to literals
if isinstance(x, jinja2.nodes.EvalContext):
(eval_ctx, x) = (x, eval_ctx)
if isinstance(x, (list, tuple)):
if isinstance(x, (list, tuple, Iterator)):
return join_original(eval_ctx, x, *args, **kwargs)
return x
template_env.filters['join'] = join_singleton
......
......@@ -1118,6 +1118,13 @@ class ListTestCase(MkexpSimpleTestCase):
['first', 'second', 'third']
""")
def test_list_on_iterator(self):
self.run_test(u"""
%{('first', 'second', 'third')|reverse|list}
""", u"""
['third', 'second', 'first']
""")
class JoinTestCase(MkexpSimpleTestCase):
def test_join_on_string(self):
......@@ -1154,6 +1161,13 @@ class JoinTestCase(MkexpSimpleTestCase):
first, second, third
""")
def test_join_on_iterator(self):
self.run_test(u"""
%{('first', 'second', 'third')|reverse|join(', ')}
""", u"""
third, second, first
""")
class IsSetTestCase(MkexpSimpleTestCase):
def test_empty_string(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment