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

Config

Jinja2 configuration.

invenio_ext.template.config.JINJA2_EXTENSIONS = [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_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.