genshi.template.base.Context

Package genshi :: Package template :: Module base :: Class Context |

# Class Context

    object --+
             |
            Context

Container for template input data.

A context provides a stack of scopes (represented by dictionaries).

Template directives such as loops can push a new scope on the stack with data that should only be available inside the loop. When the loop terminates, that scope can get popped off the stack again.

    >>> ctxt = Context(one='foo', other=1)
    >>> ctxt.get('one')
    'foo'
    >>> ctxt.get('other')
    1
    >>> ctxt.push(dict(one='frost'))
    >>> ctxt.get('one')
    'frost'
    >>> ctxt.get('other')
    1
    >>> ctxt.pop()
    {'one': 'frost'}
    >>> ctxt.get('one')
    'foo'

Instance Methods
__init__(self, **data) Initialize the template context with the given keyword arguments as data. |
__repr__(self) repr(x) |
__contains__(self, key) Return whether a variable exists in any of the scopes. |
has_key(self, key) Return whether a variable exists in any of the scopes. |
__delitem__(self, key) Remove a variable from all scopes. |
__getitem__(self, key) Get a variables's value, starting at the current scope and going upward. |
__len__(self) Return the number of distinctly named variables in the context. |
__setitem__(self, key, value) Set a variable in the current scope. |
get(self, key, default=None) Get a variable's value, starting at the current scope and going upward. |
keys(self) Return the name of all variables in the context. |
items(self) Return a list of `(name, value)` tuples for all variables in the context. |
update(self, mapping) Update the context from the mapping provided. |
push(self, data) Push a new scope on the stack. |
pop(self) Pop the top-most scope from the stack. |
copy(self) Create a copy of this Context object. |
Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Properties
Inherited from `object`: `__class__`

Method Details

### __init__(self, **data) (Constructor) |
Initialize the template context with the given keyword arguments as data. Overrides: object.__init__

### __repr__(self) (Representation operator) |
repr(x) Overrides: object.__repr__ (inherited documentation)

### __contains__(self, key) (In operator) |
Return whether a variable exists in any of the scopes. Parameters: `key` - the name of the variable

### has_key(self, key) |
Return whether a variable exists in any of the scopes. Parameters: `key` - the name of the variable

### __delitem__(self, key) (Index deletion operator) |
Remove a variable from all scopes. Parameters: `key` - the name of the variable

### __getitem__(self, key) (Indexing operator) |
Get a variables's value, starting at the current scope and going upward. Parameters: `key` - the name of the variable Returns: the variable value Raises: `KeyError` - if the requested variable wasn't found in any scope

### __len__(self) (Length operator) |
Return the number of distinctly named variables in the context. Returns: the number of variables in the context

### __setitem__(self, key, value) (Index assignment operator) |
Set a variable in the current scope. Parameters: `key` - the name of the variable `value` - the variable value

### get(self, key, default=None) |
Get a variable's value, starting at the current scope and going upward. Parameters: `key` - the name of the variable `default` - the default value to return when the variable is not found

### keys(self) |
Return the name of all variables in the context. Returns: a list of variable names

### items(self) |
Return a list of `(name, value)` tuples for all variables in the context. Returns: a list of variables

### push(self, data) |
Push a new scope on the stack. Parameters: `data` - the data dictionary to push on the context stack.