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
Provenance
- Commit
3383e1c- Tree
262611d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a8d2556
5 files changed+24 added−2 removed
modified:jinja2/testsuite/filters.py+7 added
126 unmodified lines127 env2 = Environment(autoescape=True)128 tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')129 assert tmpl.render() == '<foo><span>foo</span>'130 131+ 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'137+138 def test_last(self):139 tmpl = env.from_string('''{{ foo|last }}''')140 out = tmpl.render(foo=range(10))141 assert out == '9'208 unmodified linesmodified:jinja2/filters.py+13 added-1 removed
254 unmodified lines255 return value256 257 258 @evalcontextfilter259-def do_join(eval_ctx, value, d=u''):259+def 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 lines267 -> 1|2|3268 269 {{ [1, 2, 3]|join }}270 -> 123271+272+ It is also possible to join certain attributes of an object:273+274+ .. sourcecode:: jinja275+276+ {{ users|join(', ', attribute='username') }}277+278+ .. 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)283+284 # 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 linesmodified:jinja2/nodes.py+1 added
75 unmodified lines76 to it in extensions.77 """78 79 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.autoescape826 unmodified linesmodified:jinja2/runtime.py-1 removed
6 unmodified lines7 8 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD.10 """11-import sys11 from itertools import chain, imap12 from jinja2.nodes import EvalContext, _context_function_types13 from jinja2.utils import Markup, partial, soft_unicode, escape, missing, \14 concat, internalcode, next, object_type_repr536 unmodified linesmodified:CHANGES+3 added
27 unmodified lines28 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.35 36 Version 2.5.537 -------------38 (re-release of 2.5.4 with built documentation removed for filesize.229 unmodified lines