{"id":3235,"library":"pyramid-jinja2","title":"Pyramid Jinja2","description":"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.","status":"active","version":"2.10.1","language":"en","source_language":"en","source_url":"https://github.com/Pylons/pyramid_jinja2","tags":["Pyramid","Jinja2","templating","web framework"],"install":[{"cmd":"pip install pyramid-jinja2","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core web framework dependency.","package":"pyramid"},{"reason":"Core templating engine dependency.","package":"jinja2"}],"imports":[{"note":"Activates the Jinja2 renderer and associated configurator directives.","symbol":"Configurator.include('pyramid_jinja2')","correct":"from pyramid.config import Configurator\nconfig = Configurator()\nconfig.include('pyramid_jinja2')"},{"note":"Used to register custom renderers with different file extensions or settings. Available after `config.include('pyramid_jinja2')`.","symbol":"add_jinja2_renderer","correct":"config.add_jinja2_renderer('.html', settings_prefix='jinja2.')"},{"note":"Adds a directory or asset specification to the Jinja2 environment's search path for templates.","symbol":"add_jinja2_search_path","correct":"config.add_jinja2_search_path('myapp:templates')"},{"note":"Retrieves the configured Jinja2 Environment object for direct manipulation (e.g., adding globals/filters dynamically). Requires `config.commit()` or deferred action for full configuration to apply.","symbol":"get_jinja2_environment","correct":"env = config.get_jinja2_environment()"}],"quickstart":{"code":"from wsgiref.simple_server import make_server\nfrom pyramid.config import Configurator\nfrom pyramid.response import Response\nfrom pyramid.view import view_config\n\n# Assuming a 'templates' directory exists with 'home.jinja2' in the same package\n# e.g., myapp/templates/home.jinja2\n\n@view_config(route_name='home', renderer='home.jinja2')\ndef home_view(request):\n    return {'project': 'pyramid_jinja2 example', 'name': request.matchdict.get('name', 'World')}\n\n@view_config(route_name='hello', renderer='templates/hello.jinja2')\ndef hello_view(request):\n    return {'name': request.matchdict.get('name', 'Guest')}\n\nif __name__ == '__main__':\n    with Configurator() as config:\n        config.include('pyramid_jinja2')\n        config.add_settings({'jinja2.directories': 'myapp:templates'}) # Or pass to Configurator(settings=...)\n        config.add_route('home', '/')\n        config.add_route('hello', '/hello/{name}')\n        config.scan('.') # Scans for @view_config decorators\n        app = config.make_wsgi_app()\n\n    server = make_server('0.0.0.0', 6543, app)\n    print('Serving Pyramid Jinja2 app on http://0.0.0.0:6543')\n    print(\"Try: http://localhost:6543/ and http://localhost:6543/hello/Alice\")\n    server.serve_forever()\n\n# Example template (myapp/templates/home.jinja2):\n# <h1>Welcome to {{ project }}!</h1>\n# <p>Hello, {{ name }}!</p>\n\n# Example template (myapp/templates/hello.jinja2):\n# <h1>Greetings!</h1>\n# <p>Hello, {{ name }} from a different template!</p>","lang":"python","description":"This quickstart demonstrates a basic Pyramid application using `pyramid_jinja2` to render templates. It sets up two routes with corresponding views, each rendering a different Jinja2 template. The `config.include('pyramid_jinja2')` line is crucial for activating the Jinja2 renderer, and `jinja2.directories` is configured to locate templates."},"warnings":[{"fix":"Upgrade your Python environment to 3.7 or newer before upgrading `pyramid_jinja2`.","message":"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.","severity":"breaking","affected_versions":"2.7+"},{"fix":"Ensure `config.include('pyramid_jinja2')` is present in your application's `__init__.py` or that `pyramid_jinja2` is listed in `pyramid.includes` in your .ini file.","message":"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.","severity":"breaking","affected_versions":"2.3+"},{"fix":"If you relied on bytecode caching for performance, explicitly set `jinja2.bytecode_caching = true` in your Pyramid settings. You must also implement a manual cleanup strategy (e.g., a cron job) for stale cache files if using filesystem caching.","message":"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.","severity":"gotcha","affected_versions":"1.10+"},{"fix":"Update your Jinja2 templates to use `resource_url` instead of `model_url` for generating URLs related to resources.","message":"The `model_url` filter was deprecated in version 2.7 in favor of `resource_url` to align with Pyramid's vocabulary changes.","severity":"deprecated","affected_versions":"2.7+"},{"fix":"If you define custom Jinja2 filters, use `@jinja2.pass_context` instead of `@jinja2.contextfilter` for compatibility with Jinja2 3.0 and newer.","message":"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.","severity":"gotcha","affected_versions":"Jinja2 3.0+"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}