From d34eb125c3f9b05ea12c7c3a767cd668b791334d Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 13 Oct 2008 23:47:51 +0200 Subject: [PATCH] Implemented and documented "joiner" --HG-- branch : trunk --- CHANGES | 4 +++- docs/templates.rst | 20 ++++++++++++++++++++ jinja2/defaults.py | 5 +++-- jinja2/loaders.py | 15 ++++++++++++--- jinja2/utils.py | 14 ++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index c0c7139..3b50265 100644 --- a/CHANGES +++ b/CHANGES @@ -37,7 +37,9 @@ Version 2.1 - inclusions and imports "with context" forward all variables now, not only the initial context. -- added a cycle helper called `cycle`. +- added a cycle helper called `cycler`. + +- added a joining helper called `joiner`. Version 2.0 ----------- diff --git a/docs/templates.rst b/docs/templates.rst index d797fec..ac80400 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1080,6 +1080,26 @@ The following functions are available in the global scope by default: **new in Jinja 2.1** +.. class:: joiner(sep=', ') + + A tiny helper that can be use to "join" multiple sections. A joiner is + passed a string and will return that string every time it's calld, except + the first time in which situation it returns an empty string. You can + use this to join things:: + + {% set pipe = joiner("|") %} + {% if categories %} {{ pipe() }} + Categories: {{ categories|join(", ") }} + {% endif %} + {% if author %} {{ pipe() }} + Author: {{ author() }} + {% endif %} + {% if can_edit %} {{ pipe() }} + Edit + {% endif %} + + **new in Jinja 2.1** + Extensions ---------- diff --git a/jinja2/defaults.py b/jinja2/defaults.py index 61706ae..3e24e7d 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -8,7 +8,7 @@ :copyright: 2007-2008 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ -from jinja2.utils import generate_lorem_ipsum, Cycler +from jinja2.utils import generate_lorem_ipsum, Cycler, Joiner # defaults for the parser / lexer @@ -30,7 +30,8 @@ DEFAULT_NAMESPACE = { 'range': xrange, 'dict': lambda **kw: kw, 'lipsum': generate_lorem_ipsum, - 'cycler': Cycler + 'cycler': Cycler, + 'joiner': Joiner } diff --git a/jinja2/loaders.py b/jinja2/loaders.py index 8b2221f..c61cd84 100644 --- a/jinja2/loaders.py +++ b/jinja2/loaders.py @@ -149,8 +149,14 @@ class FileSystemLoader(BaseLoader): contents = f.read().decode(self.encoding) finally: f.close() - old = path.getmtime(filename) - return contents, filename, lambda: path.getmtime(filename) == old + + mtime = path.getmtime(filename) + def uptodate(): + try: + return path.getmtime(filename) == mtime + except OSError: + return False + return contents, filename, uptodate raise TemplateNotFound(template) @@ -191,7 +197,10 @@ class PackageLoader(BaseLoader): filename = self.provider.get_resource_filename(self.manager, p) mtime = path.getmtime(filename) def uptodate(): - return path.getmtime(filename) == mtime + try: + return path.getmtime(filename) == mtime + except OSError: + return False source = self.provider.get_resource_string(self.manager, p) return source.decode(self.encoding), filename, uptodate diff --git a/jinja2/utils.py b/jinja2/utils.py index 338db4a..249e363 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -685,6 +685,20 @@ class Cycler(object): return rv +class Joiner(object): + """A joining helper for templates.""" + + def __init__(self, sep=u', '): + self.sep = sep + self.used = False + + def __call__(self): + if not self.used: + self.used = True + return u'' + return self.sep + + # we have to import it down here as the speedups module imports the # markup type which is define above. try: -- 2.26.2