Skip to content
Myr

join now also accepts parameters

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent a8d2556 · Browse files · 5 files+24 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+24 added−2 removed

  • other:+24 added−2 removed

5 files+24 added−2 removed

modified:jinja2/testsuite/filters.py+7 added
126 unmodified lines
127 env2 = Environment(autoescape=True)128 tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')129 assert tmpl.render() == '&lt;foo&gt;<span>foo</span>'130131 def test_join_attribute(self):132 class User(object):133 def __init__(self, username):134 self.username = username135 tmpl = env.from_string('''{{ users|join(', ', 'username') }}''')136 assert tmpl.render(users=map(User, ['foo', 'bar'])) == 'foo, bar'137138 def test_last(self):139 tmpl = env.from_string('''{{ foo|last }}''')140 out = tmpl.render(foo=range(10))141 assert out == '9'
208 unmodified lines
modified:jinja2/filters.py+13 added-1 removed
254 unmodified lines
255 return value256257258@evalcontextfilter259def do_join(eval_ctx, value, d=u''):259def do_join(eval_ctx, value, d=u'', attribute=None):260 """Return a string which is the concatenation of the strings in the261 sequence. The separator between elements is an empty string per262 default, you can define it with the optional parameter:263
3 unmodified lines
267 -> 1|2|3268269 {{ [1, 2, 3]|join }}270 -> 123271272 It is also possible to join certain attributes of an object:273274 .. sourcecode:: jinja275276 {{ users|join(', ', attribute='username') }}277278 .. versionadded:: 2.6279 The `attribute` parameter was added.280 """281 if attribute is not None:282 value = imap(make_attrgetter(eval_ctx.environment, attribute), value)283284 # no automatic escaping? joining is a lot eaiser then285 if not eval_ctx.autoescape:286 return unicode(d).join(imap(unicode, value))287
502 unmodified lines
modified:jinja2/nodes.py+1 added
75 unmodified lines
76 to it in extensions.77 """7879 def __init__(self, environment, template_name=None):80 self.environment = environment81 if callable(environment.autoescape):82 self.autoescape = environment.autoescape(template_name)83 else:84 self.autoescape = environment.autoescape
826 unmodified lines
modified:jinja2/runtime.py-1 removed
6 unmodified lines
78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD.10"""11import sys11from itertools import chain, imap12from jinja2.nodes import EvalContext, _context_function_types13from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \14 concat, internalcode, next, object_type_repr
536 unmodified lines
modified:CHANGES+3 added
27 unmodified lines
28 incompatible change. The argument to the filter previously was the29 optional starting index which defaultes to zero. This now became the30 second argument to the function because it's rarely used.31- like sum, sort now also makes it possible to order items by attribute.32- like sum and sort, join now also is able to join attributes of objects33 as string.34- the internal eval context now has a reference to the environment.3536Version 2.5.537-------------38(re-release of 2.5.4 with built documentation removed for filesize.
229 unmodified lines