Templates

Additional extensions and filters for jinja2 module.

invenio.ext.template.inject_utils()

Inject variables and functions to jinja execution context.

In particular it will add:

  • url_for: an Invenio specific wrapper of Flask url_for, that will let you obtain URLs for non Flask-native handlers (i.e. not yet ported Invenio URLs)
  • _: this can be used to automatically translate a given string.
  • is_language_rtl: True if the chosen language should be read right to left.
invenio.ext.template.render_template_to_string(input, _from_string=False, **context)

Render a template from the template folder with the given context.

Code based on https://github.com/mitsuhiko/flask/blob/master/flask/templating.py

Parameters:
  • input – the string template, or name of the template to be rendered, or an iterable with template names the first one existing will be rendered
  • context – the variables that should be available in the context of the template.
Returns:

a string

invenio.ext.template.setup_app(app)

Extend application template filters with custom filters and fixes.

List of applied filters:

  • filesizeformat
  • path_join
  • quoted_txt2html
  • invenio_format_date
  • invenio_pretty_date
  • invenio_url_args

Jinja2 extensions

Custom Jinja2 extensions.

class invenio.ext.template.extensions.LangExtension(environment)

Bases: jinja2.ext.Extension

Ease transition from legacy templates using <lang>...</lang>.

identifier = 'invenio.ext.template.extensions.LangExtension'
parse(parser)

Parse the template.

tags = set(['lang'])

Config

Jinja2 configuration.

invenio.ext.template.config.JINJA2_EXTENSIONS = [u'invenio.ext.template.extensions:LangExtension', u'jinja2.ext.do']

List of automatically loaded extensions.

Loader

Customization of default Flask Jinja2 template loader.

By default the Flask Jinja2 template loader is not aware of the order of Blueprints as defined by the PACKAGES configuration variable.

class invenio.ext.template.loader.OrderAwareDispatchingJinjaLoader(app)

Bases: flask.templating.DispatchingJinjaLoader

Order aware dispatching Jinja loader.

BCCache

Configuration class for jinja2.bccache.

JINJA2_BCCACHE = False
JINJA2_BCCACHE_SIZE = -1
JINJA2_BCCACHE_AUTO_RELOAD = False
JINJA2_BCCACHE_CLIENT = 'invenio.ext.cache:cache'
JINJA2_BCCACHE_PREFIX = 'jinja2::bccache::'
JINJA2_BCCACHE_TIMEOUT = None
JINJA2_BCCACHE_IGNORE_CACHE_ERRORS = True
class invenio.ext.template.bccache.BytecodeCacheWithConfig(app)

Bases: jinja2.bccache.MemcachedBytecodeCache

A bytecode cache that uses application config for initialization.

Context Processor

Additional decorator for extending template context with new objects.

invenio.ext.template.context_processor.register_template_context_processor(f)

Register globally the context processor.

invenio.ext.template.context_processor.setup_app(app)

Initialize template context processor extension.

class invenio.ext.template.context_processor.template_args(endpoint, app=None)

Bases: object

Register a context processor function for given endpoint.

If you need to pass some extra parameters to the template, you can override particular template context of any view function. Decorated function is executed for every single render_template executed within you view. For any execution heavy functions use caching per request.

These arguments are local for this request and will be discarded in the next request.

Any value passed through _invenio_template_args will override whatever parent view function passed to the template.

Example of usage in an extension:

def setup_app(app):

    @template_args('collections.index', app=app)
    def foo():
        return dict(foo='bar')

Example of usage in an overlay views.py:

from invenio.modules.collections.views.collections import index

@template_args(index)
def bar():
    return {'baz': 'bar'}

If you define endpoint as string then template_args should only be called from an extensions setup_app.

Note

Make sure that each @template_args is called (initialized) only once.

app

Return app from constructor or current_app.

endpoint

Return view function for given endpoint.