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()
|
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.
Default context to use when rendering the template
Example
1 2 3 4 | class FooView(ContextMixin):
context = {
'foo': 'bar',
}
|
Adds a new element to the context.
Parameters: |
|
---|---|
Returns: | The new context |
Return type: | dict |
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 |
Propety method which returns the current context.
Returns: | Current context value |
---|---|
Return type: | dict |
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 |
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
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 |