Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- Add ``get_jinja2_environment`` directive.

- Add all configurator directives to documentation.
  • Loading branch information...
commit d0cd14b3088ec230b00b0e943b0068c2239d6586 1 parent f371c94
Chris McDonough mcdonc authored
7 CHANGES.txt
... ... @@ -1,3 +1,10 @@
  1 +Next release
  2 +============
  3 +
  4 +- Add ``get_jinja2_environment`` directive.
  5 +
  6 +- Add all configurator directives to documentation.
  7 +
1 8 1.0 (2011-05-12)
2 9 ================
3 10
6 docs/api.rst
Source Rendered
@@ -7,5 +7,11 @@
7 7
8 8 .. autofunction:: includeme
9 9
  10 +.. autofunction:: add_jinja2_search_path
  11 +
  12 +.. autofunction:: add_jinja2_extension
  13 +
  14 +.. autofunction:: get_jinja2_environment
  15 +
10 16 .. autoclass:: Jinja2TemplateRenderer
11 17 .. autoclass:: SmartAssetSpecLoader
2  docs/conf.py
@@ -125,7 +125,7 @@
125 125 # here, relative to this directory. They are copied after the builtin
126 126 # static files, so a file named "default.css" will overwrite the builtin
127 127 # "default.css".
128   -html_static_path = ['_static']
  128 +#html_static_path = ['_static']
129 129
130 130 # If not '', a 'Last updated on:' timestamp is inserted at every page
131 131 # bottom, using the given strftime format.
15 docs/glossary.rst
Source Rendered
@@ -18,10 +18,17 @@ Glossary
18 18 <http://docs.pylonsproject.org/projects/pyramid/1.0/api/config.html#pyramid.config.Configurator>`_
19 19
20 20 pyramid_jinja2
21   - A set of bindings that make templates written for the
22   - :term:`Jinja2` templating system work under the :term:`Pyramid` web framework.
  21 + A set of bindings that make templates written for the :term:`Jinja2`
  22 + templating system work under the :term:`Pyramid` web framework.
23 23
24 24 Template Inheritance
25   - Allows you to build a base “skeleton” template that contains all the common
26   - elements of your site and defines blocks that child templates can override.
  25 + Allows you to build a base “skeleton” template that contains all the
  26 + common elements of your site and defines blocks that child templates
  27 + can override.
27 28 http://jinja.pocoo.org/docs/templates/#template-inheritance
  29 +
  30 + Asset spec
  31 + A string representing the path to a directory or file present in a
  32 + Python module. See `Understanding Asset Specifications
  33 + <http://docs.pylonsproject.org/projects/pyramid/current/narr/assets.html#understanding-asset-specifications>`_
  34 + in the Pyramid documentation for more information.
12 docs/index.rst
Source Rendered
@@ -35,8 +35,14 @@ Once activated either of these says, the following happens:
35 35 #) Files with the ``.jinja2`` extension are considered to be
36 36 :term:`Jinja2` templates.
37 37
38   -#) The ``add_jinja2_search_path`` method/directive is added to
39   - the :term:`configurator` instance.
  38 +#) The :func:`pyramid_jinja2.add_jinja2_search_path` directive is added to
  39 + the :term:`configurator` instance.
  40 +
  41 +#) The :func:`pyramid_jinja2.add_jinja2_extension` directive is added to the
  42 + :term:`configurator` instance.
  43 +
  44 +#) The :func:`pyramid_jinja2.get_jinja2_environment` directive is added to the
  45 + :term:`configurator` instance.
40 46
41 47 Usage
42 48 =====
@@ -204,7 +210,7 @@ command to create a Jinja2-based Pyramid project::
204 210 This is a good way to see a working Pyramid application that uses Jinja2, even
205 211 if you wind up not using the result.
206 212
207   -.. code-block:: shell
  213 +.. code-block:: text
208 214 :linenos:
209 215
210 216 # make sure Babel is installed
70 pyramid_jinja2/__init__.py
@@ -277,7 +277,20 @@ def __call__(self, value, system):
277 277 return self.template.render(system)
278 278
279 279
280   -def _add_jinja2_search_path(config, searchpath):
  280 +def add_jinja2_search_path(config, searchpath):
  281 + """
  282 + This function is added as a method of a :term:`Configurator`, and
  283 + should not be called directly. Instead it should be called like so after
  284 + ``pyramid_jinja2`` has been passed to ``config.include``:
  285 +
  286 + .. code-block:: python
  287 +
  288 + config.add_jinja2_search_path('anotherpackage:templates/')
  289 +
  290 + It will add the directory or :term:`asset spec` passed as ``searchpath``
  291 + to the current search path of the ``jinja2.environment.Environment`` used
  292 + by :mod:`pyramid_jinja2`.
  293 + """
281 294 registry = config.registry
282 295 env = _get_or_build_default_environment(registry)
283 296 if isinstance(searchpath, basestring):
@@ -286,7 +299,19 @@ def _add_jinja2_search_path(config, searchpath):
286 299 env.loader.searchpath.append(abspath_from_resource_spec(d))
287 300
288 301
289   -def _add_jinja2_extension(config, ext):
  302 +def add_jinja2_extension(config, ext):
  303 + """
  304 + This function is added as a method of a :term:`Configurator`, and
  305 + should not be called directly. Instead it should be called like so after
  306 + ``pyramid_jinja2`` has been passed to ``config.include``:
  307 +
  308 + .. code-block:: python
  309 +
  310 + config.add_jinja2_extension(myext)
  311 +
  312 + It will add the Jinja2 extension passed as ``ext`` to the current
  313 + ``jinja2.environment.Environment`` used by :mod:`pyramid_jinja2`.
  314 + """
290 315 registry = config.registry
291 316
292 317 lst = _get_extensions(config)
@@ -309,10 +334,45 @@ def _get_extensions(config_or_registry):
309 334 exts.append('jinja2.ext.i18n')
310 335 return exts
311 336
  337 +def get_jinja2_environment(config):
  338 + """
  339 + This function is added as a method of a :term:`Configurator`, and
  340 + should not be called directly. Instead it should be called like so after
  341 + ``pyramid_jinja2`` has been passed to ``config.include``:
  342 +
  343 + .. code-block:: python
  344 +
  345 + config.get_jinja2_environment()
  346 +
  347 + It will return the current ``jinja2.environment.Environment`` used by
  348 + :mod:`pyramid_jinja2` or ``None`` if no environment has yet been set up.
  349 + """
  350 + return config.registry.queryUtility(IJinja2Environment)
312 351
313 352 def includeme(config):
314   - '''Setup standard configurator registrations.'''
  353 + """Set up standard configurator registrations. Use via:
  354 +
  355 + .. code-block:: python
  356 +
  357 + config = Configurator()
  358 + config.include('pyramid_jinja2')
  359 +
  360 + Once this function has been invoked, the ``.jinja2`` renderer is
  361 + available for use in Pyramid and these new directives are available as
  362 + methods of the configurator:
  363 +
  364 + - ``add_jinja2_search_path``: Add a search path location to the search
  365 + path.
  366 +
  367 + - ``add_jinja2_extension``: Add a list of extensions to the Jinja2
  368 + environment.
  369 +
  370 + - get_jinja2_environment``: Return the Jinja2 ``environment.Environment``
  371 + used by ``pyramid_jinja2``.
  372 +
  373 + """
315 374 config.add_renderer('.jinja2', renderer_factory)
316   - config.add_directive('add_jinja2_search_path', _add_jinja2_search_path)
317   - config.add_directive('add_jinja2_extension', _add_jinja2_extension)
  375 + config.add_directive('add_jinja2_search_path', add_jinja2_search_path)
  376 + config.add_directive('add_jinja2_extension', add_jinja2_extension)
  377 + config.add_directive('get_jinja2_environment', get_jinja2_environment)
318 378 _get_or_build_default_environment(config.registry)
7 pyramid_jinja2/demo/__init__.py
@@ -21,10 +21,9 @@ def root_view(request):
21 21 def app(global_settings, **settings):
22 22 config = Configurator(settings=settings)
23 23 config.include('pyramid_jinja2')
24   - config.add_route(name='root',
25   - pattern='/',
26   - view=root_view,
27   - renderer='helloworld.jinja2')
  24 + config.add_route(name='root', pattern='/')
  25 + config.add_view(root_view,
  26 + renderer='helloworld.jinja2')
28 27 config.add_translation_dirs('pyramid_jinja2.demo:locale/')
29 28 return config.make_wsgi_app()
30 29
14 pyramid_jinja2/tests/test_it.py
@@ -243,6 +243,15 @@ def test_it(self):
243 243 for x in utility.loader.searchpath],
244 244 ['foobar', 'grrr'])
245 245
  246 +class Test_get_jinja2_environment(unittest.TestCase):
  247 + def test_it(self):
  248 + from jinja2.environment import Environment
  249 + from pyramid_jinja2 import includeme
  250 + config = testing.setUp()
  251 + includeme(config)
  252 + self.assertEqual(config.get_jinja2_environment().__class__,
  253 + Environment)
  254 +
246 255
247 256 class TestSmartAssetSpecLoader(unittest.TestCase):
248 257
@@ -396,7 +405,8 @@ def stack(self):
396 405 class MiscTests(Base, unittest.TestCase):
397 406
398 407 def test_add_jinja2_extension(self):
399   - from pyramid_jinja2 import _add_jinja2_extension, _get_or_build_default_environment
  408 + from pyramid_jinja2 import (add_jinja2_extension,
  409 + _get_or_build_default_environment)
400 410 self.config.include('pyramid_jinja2')
401 411
402 412 class MockExt(object):
@@ -405,7 +415,7 @@ class MockExt(object):
405 415 def __init__(self, x):
406 416 self.x = x
407 417
408   - _add_jinja2_extension(self.config, MockExt)
  418 + add_jinja2_extension(self.config, MockExt)
409 419
410 420 u = _get_or_build_default_environment(self.config.registry)
411 421 self.assertTrue('foobar' in u.extensions)

0 comments on commit d0cd14b

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