flask_velox.mixins.context

Module provides mixin classes for dealing with updating context passed to templates for rendering.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from flask.ext.velox.mixins.context import ContextMixin
from flask.ext.velox.mixins.template import TemplateMixin

app = Flask(__name__)

class MyView(TemplateMixin, ContextMixin):
    context = {
        'foo': 'bar'
    }

app.add_url_rule('/', view_func=MyView.as_view('myview'))

app.run()
class flask_velox.mixins.context.ContextMixin(*args, **kwargs)

Bases: object

Mixin this class to add context support to template rendering. Default context can be defined by setting the context attribute to contain a dict of key value pairs.

context dict, optional

Default context to use when rendering the template

Example

1
2
3
4
class FooView(ContextMixin):
    context = {
        'foo': 'bar',
    }
add_context(key, val)

Adds a new element to the context.

Parameters:
  • key (str) – The context key name
  • val – The value of the new context item of any type
Returns:

The new context

Return type:

dict

del_context(key)

Removes an element from the context dictionary.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters:key (str) – The context key name
Returns:Success or Failure
Return type:bool
Returns:The new context
Return type:dict
get_context()

Propety method which returns the current context.

Returns:Current context value
Return type:dict
merge_context(subject)

Merge the passed dictionary into the current _context

Parameters:subject (dict) – The dict to merge into _context

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
    'hello': 'world',
    'foo': 'bar'
}
Returns:The new context
Return type:dict
set_context()

A method which should be overridden when required to set extra context on a per view basis. This method is not required to be implemented however is the recommended way of setting extra context

update_context(new)

Overwrites the existing context with the provided new context value.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
    'hello': 'world'
}
Parameters:new (dict) – Value to update context too
Returns:The new context
Return type:dict

http://thisissoon.com

Related Topics

This Page