bag.web.pyramid.genshi module

Genshi integration for Pyramid.

This module allows the Genshi templating language – – to be used in the Pyramid web framework –

To enable this extension, just include it as your app starts up:


Then you can decorate your views like the other Pyramid templating languages, passing an asset specification to the renderer argument:

@view_config(route_name='faq', renderer='mial:templates/faq-page.genshi')

Configuring a set of template directories

You don’t need to do this. But if you would like to provide only the file name (without a path) to the renderer, like this:

@view_config(route_name='faq', renderer='faq-page.genshi')

…then you must set genshi.directories in the application section of your Pyramid application’s .ini file:

# ... other stuff ...
genshi.directories = myapp:templates

This configures a set of directories that are searched. The portion of each directory argument before the colon is a package name. The remainder is a subpath within the package which houses the templates.

But I have optimized for asset specifications. The renderer name is interpreted first as an asset specification, then if the file is not found, the directories are searched.

Other settings

You can configure these rendering parameters:

genshi.doctype = html5
genshi.method = xhtml

You can also set the file extension that triggers the Genshi renderer. The default is “.genshi”:

genshi.extension = .genshi

The Genshi template loader keeps templates cached in memory. You can control the size of this LRU cache through this setting (my default is 100):

genshi.max_cache_size = 100

Finally, internationalization of Genshi templates is enabled by the value of genshi.translation_domain. By default it is the name of your application package.

genshi.translation_domain = myapp

Rendering a page fragment

From anywhere in your web app you can use the renderer like this:

some_html = settings['genshi_renderer'].fragment(
    'myapp:templates/menu.genshi', template_context_dict)
class bag.web.pyramid.genshi.GenshiTemplateRenderer(settings)[source]

Bases: object

fragment(template, dic)[source]

Loads a Genshi template and returns its output as a unicode object containing an HTML fragment, taking care of some details.

  • template is the Genshi template file to be rendered.
  • dic is a dictionary to populate the template instance.

Easily integrate Genshi template rendering into Pyramid.


Make the Genshi TemplateLoader work with typical Pyramid asset specifications by passing this function to the TemplateLoader constructor as one of the paths.