- like sum and sort, join now also is able to join attributes of objects
as string.
- the internal eval context now has a reference to the environment.
+- added a mapping test to see if an object is a dict or an object with
+ a similar interface.
Version 2.5.5
-------------
import re
from jinja2.runtime import Undefined
+try:
+ from collections import Mapping as MappingType
+except ImportError:
+ import UserDict
+ MappingType = (UserDict.UserDict, UserDict.DictMixin, dict)
+
# nose, nothing here to test
__test__ = False
return isinstance(value, basestring)
+def test_mapping(value):
+ """Return true if the object is a mapping (dict etc.).
+
+ .. versionadded:: 2.6
+ """
+ return isinstance(value, MappingType)
+
+
def test_number(value):
"""Return true if the variable is a number."""
return isinstance(value, (int, long, float, complex))
'lower': test_lower,
'upper': test_upper,
'string': test_string,
+ 'mapping': test_mapping,
'number': test_number,
'sequence': test_sequence,
'iterable': test_iterable,
{{ range is callable }}
{{ 42 is callable }}
{{ range(5) is iterable }}
+ {{ {} is mapping }}
+ {{ mydict is mapping }}
+ {{ [] is mapping }}
''')
- assert tmpl.render().split() == [
+ class MyDict(dict):
+ pass
+ assert tmpl.render(mydict=MyDict()).split() == [
'False', 'True', 'False', 'True', 'True', 'False',
- 'True', 'True', 'True', 'True', 'False', 'True'
+ 'True', 'True', 'True', 'True', 'False', 'True',
+ 'True', 'True', 'False'
]
def test_sequence(self):