some more documentation updates and minor code cleanups. Additionally True and true in the template are the same now, same for false/False and none/None.
--HG--
branch : trunk
diff --git a/jinja2/utils.py b/jinja2/utils.py
index e064a25..6d1c958 100644
--- a/jinja2/utils.py
+++ b/jinja2/utils.py
@@ -63,23 +63,47 @@
def contextfunction(f):
- """This decorator can be used to mark a callable as context callable. A
- context callable is passed the active context as first argument when
- called from the template.
+ """This decorator can be used to mark a function or method context callable.
+ A context callable is passed the active :class:`Context` as first argument when
+ called from the template. This is useful if a function wants to get access
+ to the context or functions provided on the context object. For example
+ a function that returns a sorted list of template variables the current
+ template exports could look like this::
+
+ @contextcallable
+ def get_exported_names(context):
+ return sorted(context.exported_vars)
"""
f.contextfunction = True
return f
def environmentfunction(f):
- """This decorator can be used to mark a callable as environment callable.
- A environment callable is passed the current environment as first argument
- when called from the template.
+ """This decorator can be used to mark a function or method as environment
+ callable. This decorator works exactly like the :func:`contextfunction`
+ decorator just that the first argument is the active :class:`Environment`
+ and not context.
"""
f.environmentfunction = True
return f
+def is_undefined(obj):
+ """Check if the object passed is undefined. This does nothing more than
+ performing an instance check against :class:`Undefined` but looks nicer.
+ This can be used for custom filters or tests that want to react to
+ undefined variables. For example a custom default filter can look like
+ this::
+
+ def default(var, default=''):
+ if is_undefined(var):
+ return default
+ return var
+ """
+ from jinja2.runtime import Undefined
+ return isinstance(obj, Undefined)
+
+
def clear_caches():
"""Jinja2 keeps internal caches for environments and lexers. These are
used so that Jinja2 doesn't have to recreate environments and lexers all
@@ -532,6 +556,14 @@
__copy__ = copy
+# register the LRU cache as mutable mapping if possible
+try:
+ from collections import MutableMapping
+ MutableMapping.register(LRUCache)
+except ImportError:
+ pass
+
+
# we have to import it down here as the speedups module imports the
# markup type which is define above.
try: