Pyramid Jinja2
Pyramid Jinja2 is a set of bindings that integrate the Jinja2 templating system with the Pyramid web framework. Currently at version 2.10.1, it is actively maintained by the Pylons Project, offering robust template rendering capabilities for Pyramid applications.
Warnings
- breaking Python 2.x and older Python 3.x versions are no longer supported. Version 2.7 dropped Python 2.6 and 3.2, Version 2.8 dropped Python 3.3, and Version 2.9 dropped Python 3.6. The current version requires Python >=3.7.0.
- breaking As of `pyramid_jinja2` 2.3, using `pyramid_jinja2.renderer_factory` now explicitly requires `pyramid_jinja2` to be included in the Configurator (via `config.include('pyramid_jinja2')` or `pyramid.includes` setting). Previously, it could be used without explicit inclusion.
- gotcha The default behavior of `jinja2.bytecode_caching` changed in version 1.10 from `true` to `false`. Additionally, the automatic `atexit` callback for clearing bytecode cache files was removed.
- deprecated The `model_url` filter was deprecated in version 2.7 in favor of `resource_url` to align with Pyramid's vocabulary changes.
- gotcha For custom Jinja2 filters, Jinja2 >= 3.0 deprecated `@jinja2.contextfilter` in favor of `@jinja2.pass_context`. `pyramid_jinja2` version 2.9.2 and later supports both, but newer Jinja2 versions will warn or break with `contextfilter` usage.
Install
-
pip install pyramid-jinja2
Imports
- Configurator.include('pyramid_jinja2')
from pyramid.config import Configurator config = Configurator() config.include('pyramid_jinja2') - add_jinja2_renderer
config.add_jinja2_renderer('.html', settings_prefix='jinja2.') - add_jinja2_search_path
config.add_jinja2_search_path('myapp:templates') - get_jinja2_environment
env = config.get_jinja2_environment()
Quickstart
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
# Assuming a 'templates' directory exists with 'home.jinja2' in the same package
# e.g., myapp/templates/home.jinja2
@view_config(route_name='home', renderer='home.jinja2')
def home_view(request):
return {'project': 'pyramid_jinja2 example', 'name': request.matchdict.get('name', 'World')}
@view_config(route_name='hello', renderer='templates/hello.jinja2')
def hello_view(request):
return {'name': request.matchdict.get('name', 'Guest')}
if __name__ == '__main__':
with Configurator() as config:
config.include('pyramid_jinja2')
config.add_settings({'jinja2.directories': 'myapp:templates'}) # Or pass to Configurator(settings=...)
config.add_route('home', '/')
config.add_route('hello', '/hello/{name}')
config.scan('.') # Scans for @view_config decorators
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
print('Serving Pyramid Jinja2 app on http://0.0.0.0:6543')
print("Try: http://localhost:6543/ and http://localhost:6543/hello/Alice")
server.serve_forever()
# Example template (myapp/templates/home.jinja2):
# <h1>Welcome to {{ project }}!</h1>
# <p>Hello, {{ name }}!</p>
# Example template (myapp/templates/hello.jinja2):
# <h1>Greetings!</h1>
# <p>Hello, {{ name }} from a different template!</p>