Skip to content
Browse files

Merge branch 'master' of git://github.com/Pylons/pyramid_jinja2

Conflicts:
	docs/index.rst
	pyramid_jinja2/__init__.py
  • Loading branch information...
2 parents f0ed8ec + e7dd9c3 commit faf62acf1894f215db75b7c051b09fa81d9bf9cf Michael R committed Nov 15, 2012
View
28 CHANGES.txt
@@ -1,13 +1,39 @@
-1.4.2 (unreleased)
+1.4.3 (unreleased)
==================
+- Add support for more Jinja2 options. Note support for jinja2.autoescape is
+ limitted to boolean only.
+ * jinja2.block_start_string
+ * jinja2.block_end_string
+ * jinja2.variable_start_string
+ * jinja2.variable_end_string
+ * jinja2.comment_start_string
+ * jinja2.comment_end_string
+ * jinja2.line_statement_prefix
+ * jinja2.line_comment_prefix
+ * jinja2.trim_blocks
+ * jinja2.newline_sequence
+ * jinja2.optimized
+ * jinja2.cache_size
+ * jinja2.autoescape
+ [Michael Ryabushkin]
+
+1.4.2 (2012-10-17)
+==================
+
+- Add `jinja2.undefined` setting to change handling of undefined types.
+ [Robert Buchholz]
+
- Remove redundant decoding error handling
[Domen Kožar]
- Configure bytecode caching by default. Introduce `jinja2.bytecode_caching`
and `jinja2.bytecode_caching_directory` settings.
[Domen Kožar]
+- Allow to add custom Jinja2 tests in `jinja2.tests` setting.
+ [Sebastian Kalinowski]
+
1.4.1 (2012-09-12)
==================
View
5 CONTRIBUTORS.txt
@@ -119,3 +119,8 @@ Kiall Mac Innes, 2012/05/13
Domen Kožar, 2012/09/11
Joao Paulo F Farias, 2012/09/11
+
+Sebastian Kalinowski, 2012/10/14
+
+Michael Ryabushkin, 2012/11/15
+
View
2 README.rst
@@ -1,5 +1,5 @@
`Jinja2 <http://http://jinja.pocoo.org>`_ bindings for Pyramid
-============================================================
+==============================================================
These are bindings for the `Jinja2 templating system
<http://http://jinja.pocoo.org>`_ for the `Pyramid
View
6 docs/conf.py
@@ -195,6 +195,12 @@
# Output file base name for HTML help builder.
htmlhelp_basename = 'atemplatedoc'
+# for cross referencing documentations
+intersphinx_mapping = {
+ 'jinja2': ('http://jinja.pocoo.org/docs/', None),
+ 'pyramid': ('http://docs.pylonsproject.org/projects/pyramid/en/latest/', None),
+}
+
# Options for LaTeX output
# ------------------------
View
13 docs/glossary.rst
@@ -14,8 +14,7 @@ Glossary
A `web framework <http://pylonshq.com/pyramid>`_.
Configurator
- `pyramid.config.Configurator
- <http://docs.pylonsproject.org/projects/pyramid/1.0/api/config.html#pyramid.config.Configurator>`_
+ :py:class:`pyramid.config.Configurator`
pyramid_jinja2
A set of bindings that make templates written for the :term:`Jinja2`
@@ -24,11 +23,11 @@ Glossary
Template Inheritance
Allows you to build a base “skeleton” template that contains all the
common elements of your site and defines blocks that child templates
- can override.
- http://jinja.pocoo.org/docs/templates/#template-inheritance
+ can override. See :ref:`Template Inheritance
+ <jinja2:template-inheritance>` in the Jinja2 documentation.
Asset spec
A string representing the path to a directory or file present in a
- Python module. See `Understanding Asset Specifications
- <http://docs.pylonsproject.org/projects/pyramid/current/narr/assets.html#understanding-asset-specifications>`_
- in the Pyramid documentation for more information.
+ Python module. See :ref:`Understanding Asset Specifications
+ <pyramid:asset_specifications>` in the Pyramid documentation for
+ more information.
View
238 docs/index.rst
@@ -2,128 +2,144 @@
pyramid_jinja2
==============
+.. _overview:
+
Overview
========
:term:`pyramid_jinja2` is a set of bindings that make templates written for the
:term:`Jinja2` templating system work under the :term:`Pyramid` web framework.
+.. _instalation:
+
Installation
============
Install using setuptools, e.g. (within a virtualenv)::
$ $myvenv/bin/easy_install pyramid_jinja2
+.. _setup:
+
Setup
=====
There are two ways to make sure that ``pyramid_jinja2`` is active. Both
are completely equivalent:
-#) Use the ``includeme`` function via ``config.include``::
+#) Use the :py:func:`~pyramid_jinja2.includeme` function via :py:meth:`~pyramid.config.Configurator.include`::
+ config = Configurator()
config.include('pyramid_jinja2')
-#) If you're using ``pyramid_zcml`` instead of imperative configuration,
- wnsure that some ZCML file with an analogue of the following contents is
- executed by your Pyramid application::
- <include package="pyramid_jinja2"/>
+#) If you're using `pyramid_zcml
+ <http://docs.pylonsproject.org/projects/pyramid_zcml/en/latest/>`_
+ instead of imperative configuration, ensure that some ZCML file with an
+ analogue of the following contents is executed by your Pyramid
+ application:
+
+ .. code-block:: xml
+
+ <include package="pyramid_jinja2"/>
Once activated either of these says, the following happens:
-#) Files with the ``.jinja2`` extension are considered to be
+#) Files with the :file:`.jinja2` extension are considered to be
:term:`Jinja2` templates.
#) The :func:`pyramid_jinja2.add_jinja2_search_path` directive is added to
- the :term:`configurator` instance.
+ the :term:`Configurator` instance.
#) The :func:`pyramid_jinja2.add_jinja2_extension` directive is added to the
- :term:`configurator` instance.
+ :term:`Configurator` instance.
#) The :func:`pyramid_jinja2.get_jinja2_environment` directive is added to the
- :term:`configurator` instance.
+ :term:`Configurator` instance.
-#) `jinja2.Environment` is constructed and registered globally.
+#) :py:class:`jinja2.Environment` is constructed and registered globally.
-To setup the jinja2 search path either one of the following steps must be taken:
+To setup the Jinja2 search path either one of the following steps must be taken:
-#) Add ``jinja2.directories`` to your ``.ini`` settings file using the pyramid
- asset spec::
+#) Add :ref:`setting_jinja2_directories` to your :file:`.ini` settings file using the pyramid asset spec::
jinja2.directories = yourapp:templates
-#) Or Alternatively by using the ``add_jinja2_search_path`` directive
- attached to your application's :term:`configurator` instance also using
- the pyramid asset spec::
+#) Or Alternatively by using the :func:`~pyramid_jinja2.add_jinja2_search_path`
+ directive attached to your application's :term:`Configurator` instance also
+ using the pyramid asset spec::
config.add_jinja2_search_path("yourapp:templates")
.. warning::
- If you do not explicitly configure your jinja2 search path it will
+ If you do not explicitly configure your Jinja2 search path it will
default to the root of your application. If the specified template
is not found in the root of your application and you did not specify
a package on the template path it will then try to load the template
path relative to the module's caller package. For example:
Without the search path configured:
- .. code-block:: text
+ .. code-block:: python
@view_config(renderer='templates/mytemplate.jinja2')
With the search path configured:
- .. code-block:: text
+ .. code-block:: python
@view_config(renderer='mytemplate.jinja2')
If you view module is in app.module.view and your template is
- under app/module/templates/mytemplate.jinja2 you can access
+ under :file:`app/module/templates/mytemplate.jinja2` you can access
that asset in a few different ways.
Using the full path:
- .. code-block:: text
+ .. code-block:: python
@view_config(renderer="module/templates/mytemplate.jinja2")
Using the package:
- .. code-block:: text
+ .. code-block:: python
@view_config(renderer="app.module:templates/mytemplate.jinja2")
Using the relative path to current package:
- .. code-block:: text
+ .. code-block:: python
@view_config(renderer="templates/mytemplate.jinja2")
You need to be careful when using relative paths though, if
- there is an app/templates/mytemplate.jinja2 this will be
- used instead as jinja2 lookup will first try the path relative
+ there is an :file:`app/templates/mytemplate.jinja2` this will be
+ used instead as Jinja2 lookup will first try the path relative
to the root of the app and then it will try the path relative
to the current package.
+.. _usage:
+
Usage
=====
-Once :term:`pyramid_jinja2` been activated ``.jinja2`` templates
+Once :term:`pyramid_jinja2` been activated :file:`.jinja2` templates
can be loaded either by looking up names that would be found on
the :term:`Jinja2` search path or by looking up asset specifications.
+.. _template_lookups:
+
Template Lookups
----------------
The default lookup mechanism for templates uses the :term:`Jinja2`
-search path. (specified with ``jinja2.directories`` or by using the
-add_jinja2_search_path directive on the :term:`configurator` instance.)
+search path (specified with :ref:`setting_jinja2_directories` or by using
+the :func:`~pyramid_jinja2.add_jinja2_search_path` directive on the
+:term:`Configurator` instance).
Rendering :term:`Jinja2` templates with a view like this is typically
-done as follows (where the ``templates`` directory is expected to
+done as follows (where the :file:`templates` directory is expected to
live in the search path):
.. code-block:: python
@@ -177,63 +193,42 @@ template lookups. An example:
</p>
{% endblock %}
-For further information on :term:`template inheritance` in Jinja2
-templates please see http://jinja.pocoo.org/docs/templates/#template-inheritance.
+For further information on :term:`Template Inheritance` in Jinja2
+templates please see :ref:`Template Inheritance <jinja2:template-inheritance>`
+in Jinja2 documentation.
+
+.. _assets_spec_lookups:
Asset Specification Lookups
---------------------------
Looking up templates via asset specification is a feature specific
-to :term:`Pyramid`. For further info please see `Understanding
-Asset Specifications
-<http://docs.pylonsproject.org/projects/pyramid/1.0/narr/assets.html#understanding-asset-specifications>`_.
+to :term:`Pyramid`. For further info please see :ref:`Understanding
+Asset Specifications <pyramid:asset_specifications>`.
Overriding templates in this style uses the standard
-`pyramid asset overriding technique
-<http://docs.pylonsproject.org/projects/pyramid/1.0/narr/assets.html#overriding-assets>`_.
+:ref:`pyramid asset overriding technique <pyramid:overriding_assets_section>`.
+
+.. _settings:
Settings
========
-Jinja2 derives additional settings to configure its template renderer. Many
+:term:`Jinja2` derives additional settings to configure its template renderer. Many
of these settings are optional and only need to be set if they should be
-different from the default. The below values can be present in the ``.ini``
-file used to configure the Pyramid application (in the ``app`` section
-representing your Pyramid app) or they can be passed directly within the
-``settings`` argument passed to a Pyramid Configurator.
-
-reload_templates
-
- ``true`` or ``false`` representing whether Jinja2 templates should be
- reloaded when they change on disk. Useful for development to be ``true``.
- This is the same as Jinja2's ``auto_reload`` flag.
-
-jinja2.directories
-
- A list of directory names or a newline-delimited string with each line
- representing a directory name. These locations are where Jinja2 will
- search for templates. Each can optionally be an absolute resource
- specification (e.g. ``package:subdirectory/``).
-
-jinja2.input_encoding
+different from the default. The below values can be present in the
+:file:`.ini` file used to configure the Pyramid application (in the ``app``
+section representing your Pyramid app) or they can be passed directly within
+the ``settings`` argument passed to a Pyramid Configurator.
- The input encoding of templates. Defaults to ``utf-8``.
-
-jinja2.extensions
-
- A list of extension objects or a newline-delimited set of dotted import
- locations where each line represents an extension. `jinja2.ext.i18n` is
- automatically activated.
-
-jinja2.i18n.domain
+Generic Settings
+----------------
- Pyramid domain for translations. See
- http://pyramid.readthedocs.org/en/latest/glossary.html#term-translation-domain
+ These setttings correspond to the ones documented in Jinja2.
+ Set them accordingly. See http://jinja.pocoo.org/docs/api/#high-level-api
-jinja2.filters
+.. warning::
- A dictionary mapping filter name to filter object, or a newline-delimted
- string with each line in the format ``name = dotted.name.to.filter``
- representing Jinja2 filters.
+ For the boolean settings please use ``true`` or ``false``
jinja2.block_start_string
@@ -245,6 +240,8 @@ jinja2.variable_end_string
jinja2.comment_start_string
+jinja2.comment_end_string
+
jinja2.line_statement_prefix
jinja2.line_comment_prefix
@@ -259,30 +256,107 @@ jinja2.autoescape
jinja2.cache_size
- The above setttings correspond to the ones documented in Jinja2.
- Set them accordingly. See http://jinja.pocoo.org/docs/api/#high-level-api
-.. warning::
+.. _setting_reload_templates:
- For the boolean settings please use ``true`` or ``false``
+reload_templates
+----------------
+
+``true`` or ``false`` representing whether Jinja2 templates should be
+reloaded when they change on disk. Useful for development to be ``true``.
+
+.. _setting_jinja2_autoreload:
jinja2.auto_reload
+------------------
+
+See ``reload_templates`` settings.
+
+
+.. _setting_jinja2_directories:
+
+jinja2.directories
+------------------
+
+A list of directory names or a newline-delimited string with each line
+representing a directory name. These locations are where Jinja2 will
+search for templates. Each can optionally be an absolute resource
+specification (e.g. ``package:subdirectory/``).
+
+.. _setting_jinja2_input_encoding:
+
+jinja2.input_encoding
+---------------------
+
+The input encoding of templates. Defaults to ``utf-8``.
+
+.. _setting_jinja2_undefined:
+
+jinja2.undefined
+----------------
+
+Changes the undefined types that are used when a variable name lookup fails.
+If unset, defaults to :py:class:`~jinja2.Undefined` (silent ignore). Setting
+it to ``strict`` will trigger :py:class:`~jinja2.StrictUndefined` behavior
+(raising an error, this is recommended for development). Setting it to
+``debug`` will trigger :py:class:`~jinja2.DebugUndefined`, which outputs
+debug information in some cases. See `Undefined Types <http://jinja.pocoo.org/docs/api/#undefined-types>`_
+
+.. _setting_jinja2_extensions:
+
+jinja2.extensions
+-----------------
+A list of extension objects or a newline-delimited set of dotted import
+locations where each line represents an extension. :ref:`jinja2.ext.i18n
+<jinja2:i18n-extension>` is automatically activated.
+
+.. _setting_jinja2_i18n_domain:
+
+jinja2.i18n.domain
+------------------
+Pyramid domain for translations. See :term:`Translation Domain` in Pyramid
+documentation.
+
+.. _setting_jinja2_filers:
+
+jinja2.filters
+--------------
+
+A dictionary mapping filter name to filter object, or a newline-delimted
+string with each line in the format::
+
+ name = dotted.name.to.filter
+
+representing :ref:`Jinja2 filters <jinja2:writing-filters>`.
+
+.. _setting_jinja2_tests:
+
+jinja2.tests
+------------
+A dictionary mapping test name to test object, or a newline-delimted
+string with each line in the format::
+
+ name = dotted.name.to.test
- See ``reload_templates`` settings.
+representing :ref:`Jinja2 tests <jinja2:writing-tests>`.
+.. _setting_jinja2_byte_cache:
jinja2.bytecode_caching
+-----------------------
+``true`` or ``false`` to enable filesystem bytecode caching. Defaults to
+``true``. See :ref:`Bytecode Cache <jinja2:bytecode-cache>` in Jinja2
+documentation.
- ``true`` or ``false`` to enable filesystem bytecode caching. Defaults to
- ``true``.
- See http://jinja.pocoo.org/docs/api/?highlight=bytecode#bytecode-cache
+.. _setting_jinja2_byte_cache_dir:
jinja2.bytecode_caching_directory
+---------------------------------
+Absolute path to directory to store bytecode caching files. Defaults to
+temporary directory. See :py:class:`jinja2.FileSystemBytecodeCache`.
- Absolute path to directory to store bytecode caching files. Defaults to
- temporary directory. See
- http://jinja.pocoo.org/docs/api/?highlight=bytecode#jinja2.FileSystemBytecodeCache
+.. _jinja2_filters:
Jinja2 Filters
==============
View
42 pyramid_jinja2/__init__.py
@@ -7,6 +7,9 @@
from jinja2 import Environment
from jinja2 import FileSystemBytecodeCache
+from jinja2 import Undefined
+from jinja2 import StrictUndefined
+from jinja2 import DebugUndefined
from jinja2.exceptions import TemplateNotFound
from jinja2.loaders import FileSystemLoader
@@ -38,15 +41,19 @@ def splitlines(s):
return filter(None, [x.strip() for x in s.splitlines()])
-def parse_filters(filters):
+def parse_config(config):
+ """
+ Parses config values from .ini file and returns a dictionary with
+ imported objects
+ """
# input must be a string or dict
result = {}
- if isinstance(filters, string_types):
- for f in splitlines(filters):
+ if isinstance(config, string_types):
+ for f in splitlines(config):
name, impl = f.split('=', 1)
result[name.strip()] = maybe_import_string(impl)
else:
- for name, impl in filters.items():
+ for name, impl in config.items():
result[name] = maybe_import_string(impl)
return result
@@ -57,6 +64,14 @@ def parse_multiline(extensions):
return list(extensions) # py3
+def parse_undefined(undefined):
+ if undefined == 'strict':
+ return StrictUndefined
+ if undefined == 'debug':
+ return DebugUndefined
+ return Undefined
+
+
class FileInfo(object):
open_if_exists = staticmethod(open_if_exists)
getmtime = staticmethod(os.path.getmtime)
@@ -186,8 +201,9 @@ def _get_or_build_default_environment(registry):
# set string settings
for short_key_name in ('block_start_string', 'block_end_string',
'variable_start_string', 'variable_end_string',
- 'comment_start_string', 'line_statement_prefix',
- 'line_comment_prefix', 'newline_sequence'):
+ 'comment_start_string', 'comment_end_string',
+ 'line_statement_prefix', 'line_comment_prefix',
+ 'newline_sequence'):
key_name = 'jinja2.%s' % (short_key_name,)
if key_name in settings:
kw[short_key_name] = settings.get(key_name)
@@ -208,6 +224,8 @@ def _get_or_build_default_environment(registry):
if 'jinja2.ext.i18n' not in extensions:
extensions.append('jinja2.ext.i18n')
+ undefined = parse_undefined(settings.get('jinja2.undefined', ''))
+
directories = parse_multiline(settings.get('jinja2.directories') or '')
directories = [abspath_from_resource_spec(d, package) for d in directories]
loader = SmartAssetSpecLoader(
@@ -226,6 +244,7 @@ def _get_or_build_default_environment(registry):
environment = Environment(loader=loader,
auto_reload=reload_templates,
extensions=extensions,
+ undefined=undefined,
**kw)
# register pyramid i18n functions
@@ -236,9 +255,14 @@ def _get_or_build_default_environment(registry):
if package is not None:
environment._default_package = package.__name__
- filters = parse_filters(settings.get('jinja2.filters', ''))
+ #add custom jinja2 filters
+ filters = parse_config(settings.get('jinja2.filters', ''))
environment.filters.update(filters)
+ #add custom jinja2 tests
+ tests = parse_config(settings.get('jinja2.tests', ''))
+ environment.tests.update(tests)
+
registry.registerUtility(environment, IJinja2Environment)
return registry.queryUtility(IJinja2Environment)
@@ -285,7 +309,7 @@ def template(self):
try:
return self.environment.get_template(name)
except TemplateNotFound:
- if name_with_package != None:
+ if name_with_package is not None:
return self.environment.get_template(name_with_package)
else:
raise
@@ -325,7 +349,7 @@ def add_jinja2_search_path(config, searchpath):
for folder in searchpath:
env.loader.searchpath.append(abspath_from_resource_spec(folder,
- config.package_name))
+ config.package_name))
def add_jinja2_extension(config, ext):
View
6 pyramid_jinja2/filters.py
@@ -10,22 +10,22 @@
def model_url_filter(model, *elements, **kw):
"""A filter from ``model`` to a string representing the absolute URL.
- This filter calls `pyramid.url.resource_url`.
+ This filter calls :py:func:`pyramid.url.resource_url`.
"""
request = get_current_request()
return resource_url(model, request, *elements, **kw)
def route_url_filter(route_name, *elements, **kw):
"""A filter from ``route_name`` to a string representing the absolute URL.
- This filter calls `pyramid.url.route_url`.
+ This filter calls :py:func:`pyramid.url.route_url`.
"""
request = get_current_request()
return route_url(route_name, request, *elements, **kw)
def static_url_filter(path, **kw):
"""A filter from ``path`` to a string representing the absolute URL.
- This filter calls `pyramid.url.static_url`.
+ This filter calls :py:func:`pyramid.url.static_url`.
"""
request = get_current_request()
return static_url(path, request, **kw)
View
6 pyramid_jinja2/scaffolds/jinja2_starter/+package+/templates/mytemplate.jinja2
@@ -1,5 +1,5 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>The Pyramid Web Application Development Framework</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
@@ -13,7 +13,7 @@
#wrap {display:table;height:100%}
</style>
<![endif]-->
- <style>
+ <style type="text/css">
.locale { text-align: center; }
.locale-name { font-weight: bold; }
</style>
@@ -25,7 +25,7 @@
</div>
<div id="top">
<div class="top align-center">
- <img src="{{request.application_url}}/static/logo.png" width="300" height="80"/>
+ <img src="{{request.application_url}}/static/logo.png" width="300" height="80" alt="Logo"/>
<p class="app-welcome">
Welcome to <span class="app-name">{{project}}</span>, an application generated by<br/>
the Pyramid web application development framework.
View
1 pyramid_jinja2/tests/templates/tests_and_filters.jinja2
@@ -0,0 +1 @@
+{{ "some text"|my_filter }} {% if "other text" is my_test %}is not False{% endif %}
View
103 pyramid_jinja2/tests/test_it.py
@@ -14,10 +14,10 @@
def dummy_filter(value): return 'hoge'
-class Test_parse_filters(unittest.TestCase):
+class Test_parse_config(unittest.TestCase):
def _callFUT(self, value):
- from pyramid_jinja2 import parse_filters
- return parse_filters(value)
+ from pyramid_jinja2 import parse_config
+ return parse_config(value)
def test_parse_singile_line(self):
import pyramid_jinja2
@@ -224,7 +224,69 @@ def test_render_relative_to_package(self):
from pyramid.renderers import render
result = render('templates/helloworld.jinja2', {'a': 1})
self.assertEqual(result, text_('\nHello föö', 'utf-8'))
-
+
+
+class Test_filters_and_tests(Base, unittest.TestCase):
+
+ def _set_up_environ(self):
+ from pyramid_jinja2 import IJinja2Environment
+ self.config.include('pyramid_jinja2')
+ return self.config.registry.getUtility(IJinja2Environment)
+
+ def _assert_has_test(self, test_name, test_obj):
+ environ = self._set_up_environ()
+ self.assertTrue(test_name in environ.tests)
+ self.assertEqual(environ.tests[test_name], test_obj)
+
+ def _assert_has_filter(self, filter_name, filter_obj):
+ environ = self._set_up_environ()
+ self.assertTrue(filter_name in environ.filters)
+ self.assertEqual(environ.filters[filter_name], filter_obj)
+
+ def test_set_single_filter(self):
+ self.config.registry.settings['jinja2.filters'] = \
+ 'my_filter = pyramid_jinja2.tests.test_it.my_test_func'
+ self._assert_has_filter('my_filter', my_test_func)
+
+ def test_set_single_test(self):
+ self.config.registry.settings['jinja2.tests'] = \
+ 'my_test = pyramid_jinja2.tests.test_it.my_test_func'
+ self._assert_has_test('my_test', my_test_func)
+
+ def test_set_multi_filters(self):
+ self.config.registry.settings['jinja2.filters'] = \
+ 'my_filter1 = pyramid_jinja2.tests.test_it.my_test_func\n' \
+ 'my_filter2 = pyramid_jinja2.tests.test_it.my_test_func\n' \
+ 'my_filter3 = pyramid_jinja2.tests.test_it.my_test_func'
+ self._assert_has_filter('my_filter1', my_test_func)
+ self._assert_has_filter('my_filter2', my_test_func)
+ self._assert_has_filter('my_filter3', my_test_func)
+
+ def test_set_multi_tests(self):
+ self.config.registry.settings['jinja2.tests'] = \
+ 'my_test1 = pyramid_jinja2.tests.test_it.my_test_func\n' \
+ 'my_test2 = pyramid_jinja2.tests.test_it.my_test_func\n' \
+ 'my_test3 = pyramid_jinja2.tests.test_it.my_test_func'
+ self._assert_has_test('my_test1', my_test_func)
+ self._assert_has_test('my_test2', my_test_func)
+ self._assert_has_test('my_test3', my_test_func)
+
+ def test_filter_and_test_works_in_render(self):
+ import pyramid_jinja2
+ from pyramid.renderers import render
+ config = testing.setUp()
+ config.add_settings({
+ 'jinja2.directories': 'pyramid_jinja2.tests:templates',
+ 'jinja2.tests': 'my_test = pyramid_jinja2.tests.test_it.my_test_func',
+ 'jinja2.filters': 'my_filter = pyramid_jinja2.tests.test_it.my_test_func'
+ })
+ config.add_renderer('.jinja2', pyramid_jinja2.renderer_factory)
+ result = render('tests_and_filters.jinja2', {})
+ #my_test_func returs "True" - it will be render as True when usign
+ # as filter and will pass in tests
+ self.assertEqual(result, text_('True is not False', 'utf-8'))
+ testing.tearDown()
+
class Test_includeme(unittest.TestCase):
def test_it(self):
@@ -451,3 +513,36 @@ def __init__(self, x):
env_after = _get_or_build_default_environment(self.config.registry)
self.assertTrue('foobar' in env_after.extensions)
self.assertTrue(env_before is env_after)
+
+
+class UndefinedTests(Base, unittest.TestCase):
+
+ def _assert_has_undefined(self, expected_undefined):
+ from pyramid_jinja2 import IJinja2Environment
+ self.config.include('pyramid_jinja2')
+ environ = self.config.registry.getUtility(IJinja2Environment)
+ self.assertEqual(environ.undefined, expected_undefined)
+
+ def test_set_regular_undefined_when_unset(self):
+ from jinja2 import Undefined
+ self._assert_has_undefined(Undefined)
+
+ def test_set_regular_undefined_by_default(self):
+ from jinja2 import Undefined
+ self.config.registry.settings['jinja2.undefined'] = ''
+ from jinja2 import Undefined
+ self._assert_has_undefined(Undefined)
+
+ def test_set_strict_undefined(self):
+ from jinja2 import StrictUndefined
+ self.config.registry.settings['jinja2.undefined'] = 'strict'
+ self._assert_has_undefined(StrictUndefined)
+
+ def test_set_debug_undefined(self):
+ from jinja2 import DebugUndefined
+ self.config.registry.settings['jinja2.undefined'] = 'debug'
+ self._assert_has_undefined(DebugUndefined)
+
+def my_test_func(*args, **kwargs):
+ """ Used as a fake filter/test function """
+ return True
View
2 setup.py
@@ -34,7 +34,7 @@
docs_extras = ['Sphinx']
setup(name='pyramid_jinja2',
- version='1.4.2.dev0',
+ version='1.4.3.dev0',
description='Jinja2 template bindings for the Pyramid web framework',
long_description=README + '\n\n' + CHANGES,
classifiers=[

0 comments on commit faf62ac

Please sign in to comment.
Something went wrong with that request. Please try again.