Implemented and documented "joiner"
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent ccae055 · Browse files · 5 files·+52 added−6 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+52 added−6 removed
- other:+52 added−6 removed
Provenance
- Commit
d34eb12- Tree
aa79d4b- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ccae055
5 files changed+52 added−6 removed
modified:docs/templates.rst+20 added
1078 unmodified lines1079 Returns the current item.1080 1081 **new in Jinja 2.1**1082 1083+.. class:: joiner(sep=', ')1084 1085+ A tiny helper that can be use to "join" multiple sections. A joiner is1086+ passed a string and will return that string every time it's calld, except1087+ the first time in which situation it returns an empty string. You can1088+ use this to join things::1089+1090+ {% set pipe = joiner("|") %}1091+ {% if categories %} {{ pipe() }}1092+ Categories: {{ categories|join(", ") }}1093+ {% endif %}1094+ {% if author %} {{ pipe() }}1095+ Author: {{ author() }}1096+ {% endif %}1097+ {% if can_edit %} {{ pipe() }}1098+ <a href="?action=edit">Edit</a>1099+ {% endif %}1100+1101+ **new in Jinja 2.1**1102+1103+1104 Extensions1105 ----------1106 1107 The following sections cover the built-in Jinja2 extensions that may be96 unmodified linesmodified:jinja2/defaults.py+3 added-2 removed
6 unmodified lines7 8 :copyright: 2007-2008 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11-from jinja2.utils import generate_lorem_ipsum, Cycler11+from jinja2.utils import generate_lorem_ipsum, Cycler, Joiner12 13 14 # defaults for the parser / lexer15 BLOCK_START_STRING = '{%'13 unmodified lines29 DEFAULT_NAMESPACE = {30 'range': xrange,31 'dict': lambda **kw: kw,32 'lipsum': generate_lorem_ipsum,33- 'cycler': Cycler33+ 'cycler': Cycler,34+ 'joiner': Joiner35 }36 37 38 # export all constants1 unmodified linemodified:jinja2/loaders.py+12 added-3 removed
147 unmodified lines148 try:149 contents = f.read().decode(self.encoding)150 finally:151 f.close()152- old = path.getmtime(filename)153- return contents, filename, lambda: path.getmtime(filename) == old152+153+ mtime = path.getmtime(filename)154+ def uptodate():155+ try:156+ return path.getmtime(filename) == mtime157+ except OSError:158+ return False159+ return contents, filename, uptodate160 raise TemplateNotFound(template)161 162 163 class PackageLoader(BaseLoader):32 unmodified lines196 if self.filesystem_bound:197 filename = self.provider.get_resource_filename(self.manager, p)198 mtime = path.getmtime(filename)199 def uptodate():200+ try:194- return path.getmtime(filename) == mtime201+ return path.getmtime(filename) == mtime202+ except OSError:203+ return False204 205 source = self.provider.get_resource_string(self.manager, p)206 return source.decode(self.encoding), filename, uptodate207 102 unmodified linesmodified:jinja2/utils.py+14 added
683 unmodified lines684 self.pos = (self.pos + 1) % len(self.items)685 return rv686 687 688+class Joiner(object):689+ """A joining helper for templates."""690+691+ def __init__(self, sep=u', '):692+ self.sep = sep693+ self.used = False694+695+ def __call__(self):696+ if not self.used:697+ self.used = True698+ return u''699+ return self.sep700+701+702 # we have to import it down here as the speedups module imports the703 # markup type which is define above.704 try:705 from jinja2._speedups import escape, soft_unicode37 unmodified linesmodified:CHANGES+3 added-1 removed
35 unmodified lines36 37 - inclusions and imports "with context" forward all variables now, not only38 the initial context.39 40-- added a cycle helper called `cycle`.40+- added a cycle helper called `cycler`.41 42+- added a joining helper called `joiner`.43+44 Version 2.045 -----------46 (codename jinjavitus, released on July 17th 2008)47 32 unmodified lines